2018-08-23 01:53:22 +02:00
|
|
|
--------------------------------------------------------
|
2018-08-28 18:50:21 +02:00
|
|
|
-- Minetest :: Stopwatch Mod v1.2 (stopwatch)
|
2018-08-23 01:53:22 +02:00
|
|
|
--
|
|
|
|
-- See README.txt for licensing and release notes.
|
2018-08-24 23:14:35 +02:00
|
|
|
-- Copyright (c) 2018, Leslie E. Krause
|
2018-08-23 01:53:22 +02:00
|
|
|
--------------------------------------------------------
|
|
|
|
|
2018-08-28 18:50:21 +02:00
|
|
|
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
|
|
|
|
|
2018-08-23 01:53:22 +02:00
|
|
|
local clock = minetest.get_us_time
|
|
|
|
local sprintf = string.format
|
2018-08-28 18:50:21 +02:00
|
|
|
local push = table.insert
|
2018-08-23 01:53:22 +02:00
|
|
|
|
2018-08-28 18:50:21 +02:00
|
|
|
local str_count = sprintf( "** %-16s %13s %9s\n", "series", "count", "rep" )
|
|
|
|
.. string.gsub( "** \27[%dm%-16s %10.#f %2s %8dx\27[0m", "#", prec or 3 )
|
|
|
|
local str_total = sprintf( "** %-16s %13s %9s %10s %10s %10s %10s %10s\n", "series", "total", "rep", "avg", "min", "max", "med", "dev" )
|
|
|
|
.. string.gsub( "** \27[%dm%-16s %10.#f %2s %8dx %10.#f %10.#f %10.#f %10.#f %10.#f\27[0m", "#", prec or 3 )
|
2018-08-23 01:53:22 +02:00
|
|
|
|
2018-08-28 18:50:21 +02:00
|
|
|
local function S( )
|
|
|
|
ts = clock( )
|
2018-08-23 01:53:22 +02:00
|
|
|
end
|
2018-08-24 23:14:35 +02:00
|
|
|
local function S_( is_show )
|
2018-08-28 18:50:21 +02:00
|
|
|
if not ts then error( "Trial not initiated, aborting." ) end
|
|
|
|
|
|
|
|
local v = clock( ) - ts
|
|
|
|
push( trials, v )
|
2018-08-23 01:53:22 +02:00
|
|
|
if is_show then
|
2018-08-28 18:50:21 +02:00
|
|
|
print( sprintf( str_count, escape, id, v / factor, scale or "ms", #trials ) )
|
2018-08-23 01:53:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
minetest.register_on_shutdown( function ( )
|
2018-08-28 18:50:21 +02:00
|
|
|
local v_sum = 0
|
|
|
|
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
|
|
|
|
v_sum = v_sum + v
|
|
|
|
v_min = v_min and math.min( v_min, v ) or v
|
|
|
|
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 ) )
|
2018-08-23 01:53:22 +02:00
|
|
|
end
|
|
|
|
end )
|
|
|
|
return S, S_
|
|
|
|
end
|