[Mod] Stopwatch (Benchmarking Utility) [stopwatch]
Posted: Wed Aug 22, 2018 23:42
Stopwatch Mod v1.2
stopwatch (by sorcerykid)
Stopwatch is a utility for developers to very quickly benchmark portions of Lua code, but without the complexity and overhead of enabling the mod profiler. I created this mod because my code was becoming littered with far too many time-keeping calculations while debugging. It should be fully compatible with all versions of Minetest 0.4.x+ since it only depends on the monotonic clock function.
Repository:
https://bitbucket.org/sorcerykid/stopwatch
Download Archive (.zip)
Download Archive (.tar.gz)
Dependencies:
None
Source Code License:
The MIT License (MIT)
Installation:
You will first need to create a Stopwatch object at the top of the mod that you want to benchmark. The constructor accepts four parameters: a unique identifier for the test series (required), the output color of the results (default is "white"), the numeric scale of each value (default is "ms"), and the numeric precision of each value (default is 3)
In this example, I'm measuring the performance of Lua's table.insert function in append vs. prepend mode:
The output should look something like this.
By performing multiple iterations, it is possible to get a more representative sample for each trial.
It is worth noting that Lua's print function tends to somewhat CPU intensive. Therefore you should avoid outputting the per-trial execution times for nested test series, otherwise the results will be skewed. For linear test series, this is not a concern since the execution times are already calculated prior to being output.
stopwatch (by sorcerykid)
Stopwatch is a utility for developers to very quickly benchmark portions of Lua code, but without the complexity and overhead of enabling the mod profiler. I created this mod because my code was becoming littered with far too many time-keeping calculations while debugging. It should be fully compatible with all versions of Minetest 0.4.x+ since it only depends on the monotonic clock function.
Repository:
https://bitbucket.org/sorcerykid/stopwatch
Download Archive (.zip)
Download Archive (.tar.gz)
Dependencies:
None
Source Code License:
The MIT License (MIT)
Installation:
- Unzip the archive into the mods directory of your game
- Rename the stopwatch-master directory to "stopwatch"
- Add "stopwatch" as a dependency for any mods using the API
You will first need to create a Stopwatch object at the top of the mod that you want to benchmark. The constructor accepts four parameters: a unique identifier for the test series (required), the output color of the results (default is "white"), the numeric scale of each value (default is "ms"), and the numeric precision of each value (default is 3)
- local S, S_ = Stopwatch( id, color, scale, prec )
- S( )
S_( is_show )
- total = the total execution time for the test series.
- rep = the number of trials within the test series
- avg = the mean execution time of all the trials
- min = the minimum execution time of all the trials
- max = the maximum execution time of all the trials
- med = the median execution time of all the trials
- dev = the standard deviation of all the trials
In this example, I'm measuring the performance of Lua's table.insert function in append vs. prepend mode:
Code: Select all
local S1, S1_ = Stopwatch("table_prepend")
local S2, S2_ = Stopwatch("table_append")
local y = { }
S1( )
for x = 0, 5000 do
table.insert( y, 1, x )
end
S1_( )
y = { }
S2( "table prepend" )
for x = 0, 5000 do
table.insert( y, x )
end
S2_( )
By performing multiple iterations, it is possible to get a more representative sample for each trial.
It is worth noting that Lua's print function tends to somewhat CPU intensive. Therefore you should avoid outputting the per-trial execution times for nested test series, otherwise the results will be skewed. For linear test series, this is not a concern since the execution times are already calculated prior to being output.