2018-08-23 01:53:22 +02:00
|
|
|
--------------------------------------------------------
|
2018-08-24 23:14:35 +02:00
|
|
|
-- Minetest :: Stopwatch Mod v1.1 (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
|
|
|
--------------------------------------------------------
|
|
|
|
|
|
|
|
function Stopwatch( scale )
|
|
|
|
local clock = minetest.get_us_time
|
|
|
|
local sprintf = string.format
|
|
|
|
local getinfo = debug.getinfo
|
|
|
|
local factors = { us = 1, ms = 1000, s = 1000000 }
|
|
|
|
local origin = getinfo( 2 ).source
|
|
|
|
local trials = { }
|
2018-08-24 23:14:35 +02:00
|
|
|
local series = { }
|
2018-08-23 01:53:22 +02:00
|
|
|
|
|
|
|
if not scale then
|
|
|
|
scale = "ms"
|
|
|
|
elseif not factors[ scale ] then
|
|
|
|
error( "Invalid scale specified, aborting." )
|
|
|
|
end
|
|
|
|
|
|
|
|
local function S( desc )
|
|
|
|
local i = getinfo( 2, "lf" )
|
|
|
|
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
|
2018-08-24 23:14:35 +02:00
|
|
|
table.insert( series, id )
|
2018-08-23 01:53:22 +02:00
|
|
|
v.start = clock( )
|
|
|
|
end
|
2018-08-24 23:14:35 +02:00
|
|
|
local function S_( is_show )
|
|
|
|
local id = table.remove( series )
|
|
|
|
local v = trials[ id ]
|
2018-08-23 01:53:22 +02:00
|
|
|
local delta = clock( ) - v.start
|
|
|
|
if is_show then
|
|
|
|
print( sprintf( "** trial count = %9.3f %s @%s", delta / factors[ scale ], scale, id ) )
|
|
|
|
end
|
|
|
|
v.delta_t = v.delta_t + delta
|
|
|
|
v.count = v.count + 1
|
|
|
|
end
|
|
|
|
minetest.register_on_shutdown( function ( )
|
|
|
|
local delta_g = 0
|
|
|
|
print( "** " .. origin )
|
|
|
|
for i, v in pairs( trials ) do
|
|
|
|
print( sprintf( "** trial total = %9.3f %s %4d tries %8.3f %s each @%s",
|
|
|
|
v.delta_t / factors[ scale ], scale, v.count, v.delta_t / v.count / factors[ scale ], scale, i ) )
|
|
|
|
delta_g = delta_g + v.delta_t
|
|
|
|
end
|
|
|
|
print( sprintf( "** grand total = %9.3f %s", delta_g / factors[ scale ], scale ) )
|
|
|
|
end )
|
|
|
|
return S, S_
|
|
|
|
end
|