Build 03
- major rewrite to improve accuracy and efficiency - updated code examples with latest feature-set
This commit is contained in:
parent
97e16d7380
commit
caa01a1326
|
@ -1,4 +1,4 @@
|
||||||
Stopwatch Mod v1.1
|
Stopwatch Mod v1.2
|
||||||
By Leslie Krause
|
By Leslie Krause
|
||||||
|
|
||||||
Repository
|
Repository
|
||||||
|
@ -22,6 +22,10 @@ Version 1.1b (24-Aug-2018)
|
||||||
- included working code examples for developers
|
- included working code examples for developers
|
||||||
- implemented stack for non-linear testing of code
|
- implemented stack for non-linear testing of code
|
||||||
|
|
||||||
|
Version 1.2b (28-Aug-2018)
|
||||||
|
- major rewrite to improve accuracy and efficiency
|
||||||
|
- updated code examples with latest feature-set
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
|
|
27
example.lua
27
example.lua
|
@ -1,17 +1,22 @@
|
||||||
-- measure the performance of table inserts in append vs prepend mode:
|
-- measure the performance of table inserts in append vs prepend mode:
|
||||||
|
|
||||||
local S, S_ = Stopwatch( )
|
local S1, S1_ = Stopwatch("table_prepend", "cyan")
|
||||||
|
local S2, S2_ = Stopwatch("table_append", "yellow")
|
||||||
|
|
||||||
local y = { }
|
for trial = 0, 5 do
|
||||||
S( "table prepend" )
|
local y = {}
|
||||||
for x = 0, 5000 do
|
S1()
|
||||||
table.insert( y, 1, x )
|
for x = 0, 5000 do
|
||||||
|
table.insert(y, 1, x)
|
||||||
|
end
|
||||||
|
S1_(1)
|
||||||
end
|
end
|
||||||
S_( )
|
|
||||||
|
|
||||||
y = { }
|
for trial = 0, 5 do
|
||||||
S( "table append" )
|
local y = {}
|
||||||
for x = 0, 5000 do
|
S2()
|
||||||
table.insert( y, x )
|
for x = 0, 5000 do
|
||||||
|
table.insert(y, x)
|
||||||
|
end
|
||||||
|
S2_(1)
|
||||||
end
|
end
|
||||||
S_( )
|
|
||||||
|
|
74
init.lua
74
init.lua
|
@ -1,55 +1,61 @@
|
||||||
--------------------------------------------------------
|
--------------------------------------------------------
|
||||||
-- Minetest :: Stopwatch Mod v1.1 (stopwatch)
|
-- Minetest :: Stopwatch Mod v1.2 (stopwatch)
|
||||||
--
|
--
|
||||||
-- See README.txt for licensing and release notes.
|
-- See README.txt for licensing and release notes.
|
||||||
-- Copyright (c) 2018, Leslie E. Krause
|
-- Copyright (c) 2018, Leslie E. Krause
|
||||||
--------------------------------------------------------
|
--------------------------------------------------------
|
||||||
|
|
||||||
function Stopwatch( scale )
|
function Stopwatch( id, color, scale, prec )
|
||||||
|
local ts
|
||||||
|
local trials = { }
|
||||||
|
local factor = scale and ( { us = 1, ms = 1000, s = 1000000 } )[ scale ] or 1000
|
||||||
|
local escape = color and ( { none = 0, black = 30, red = 31, green = 32, yellow = 33, blue = 34, magenta = 35, cyan = 36, white = 37 } )[ color ] or 0
|
||||||
|
|
||||||
|
if not factor then error( "Unknown scale specified, aborting." ) end
|
||||||
|
if not escape then error( "Unknown color specified, aborting." ) end
|
||||||
|
if not id then error( "Invalid id specified, aborting." ) end
|
||||||
|
|
||||||
local clock = minetest.get_us_time
|
local clock = minetest.get_us_time
|
||||||
local sprintf = string.format
|
local sprintf = string.format
|
||||||
local getinfo = debug.getinfo
|
local push = table.insert
|
||||||
local factors = { us = 1, ms = 1000, s = 1000000 }
|
|
||||||
local origin = getinfo( 2 ).source
|
|
||||||
local trials = { }
|
|
||||||
local series = { }
|
|
||||||
|
|
||||||
if not scale then
|
local str_count = sprintf( "** %-16s %13s %9s\n", "series", "count", "rep" )
|
||||||
scale = "ms"
|
.. string.gsub( "** \27[%dm%-16s %10.#f %2s %8dx\27[0m", "#", prec or 3 )
|
||||||
elseif not factors[ scale ] then
|
local str_total = sprintf( "** %-16s %13s %9s %10s %10s %10s %10s %10s\n", "series", "total", "rep", "avg", "min", "max", "med", "dev" )
|
||||||
error( "Invalid scale specified, aborting." )
|
.. string.gsub( "** \27[%dm%-16s %10.#f %2s %8dx %10.#f %10.#f %10.#f %10.#f %10.#f\27[0m", "#", prec or 3 )
|
||||||
end
|
|
||||||
|
|
||||||
local function S( desc )
|
local function S( )
|
||||||
local i = getinfo( 2, "lf" )
|
ts = clock( )
|
||||||
id = desc or tostring( i.func ) .. ", line " .. i.currentline
|
|
||||||
local v = trials[ id ]
|
|
||||||
if not v then
|
|
||||||
v = { count = 0, delta_t = 0 }
|
|
||||||
trials[ id ] = v
|
|
||||||
end
|
|
||||||
table.insert( series, id )
|
|
||||||
v.start = clock( )
|
|
||||||
end
|
end
|
||||||
local function S_( is_show )
|
local function S_( is_show )
|
||||||
local id = table.remove( series )
|
if not ts then error( "Trial not initiated, aborting." ) end
|
||||||
local v = trials[ id ]
|
|
||||||
local delta = clock( ) - v.start
|
local v = clock( ) - ts
|
||||||
|
push( trials, v )
|
||||||
if is_show then
|
if is_show then
|
||||||
print( sprintf( "** trial count = %9.3f %s @%s", delta / factors[ scale ], scale, id ) )
|
print( sprintf( str_count, escape, id, v / factor, scale or "ms", #trials ) )
|
||||||
end
|
end
|
||||||
v.delta_t = v.delta_t + delta
|
|
||||||
v.count = v.count + 1
|
|
||||||
end
|
end
|
||||||
minetest.register_on_shutdown( function ( )
|
minetest.register_on_shutdown( function ( )
|
||||||
local delta_g = 0
|
local v_sum = 0
|
||||||
print( "** " .. origin )
|
local v_var = 0
|
||||||
|
local v_min, v_max, v_med, v_avg, v_dev
|
||||||
|
|
||||||
|
if #trials > 0 then
|
||||||
for i, v in pairs( trials ) do
|
for i, v in pairs( trials ) do
|
||||||
print( sprintf( "** trial total = %9.3f %s %4d tries %8.3f %s each @%s",
|
v_sum = v_sum + v
|
||||||
v.delta_t / factors[ scale ], scale, v.count, v.delta_t / v.count / factors[ scale ], scale, i ) )
|
v_min = v_min and math.min( v_min, v ) or v
|
||||||
delta_g = delta_g + v.delta_t
|
v_max = v_max and math.max( v_max, v ) or v
|
||||||
|
end
|
||||||
|
v_avg = v_sum / #trials
|
||||||
|
for i, v in pairs( trials ) do
|
||||||
|
v_var = v_var + math.pow( v - v_avg, 2 )
|
||||||
|
end
|
||||||
|
v_dev = math.sqrt( v_var / #trials )
|
||||||
|
v_med = v_min + ( v_max - v_min ) / 2
|
||||||
|
|
||||||
|
print( sprintf( str_total, escape, id, v_sum / factor, scale or "ms", #trials, v_avg / factor, v_min / factor, v_max / factor, v_med / factor, v_dev / factor ) )
|
||||||
end
|
end
|
||||||
print( sprintf( "** grand total = %9.3f %s", delta_g / factors[ scale ], scale ) )
|
|
||||||
end )
|
end )
|
||||||
return S, S_
|
return S, S_
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue