Created
July 19, 2024 23:55
-
-
Save appgurueu/0fcbe99941064b639c014c2d43318730 to your computer and use it in GitHub Desktop.
Minetest random benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
collectgarbage"stop" -- we don't want GC heuristics to interfere | |
local n = 1e8 -- number of runs | |
local function bench(name, constructor, invokation) | |
local func = assert(loadstring(([[ | |
local r = %s | |
for _ = 1, %d do %s end | |
]]):format(constructor, n, invokation))) | |
local t = minetest.get_us_time() | |
func() | |
print(name, (minetest.get_us_time() - t) / n, "µs/call") | |
end | |
bench("Lua", "nil", "math.random()") | |
bench("PCG", "PcgRandom(42)", "r:next()") | |
bench("K&R", "PseudoRandom(42)", "r:next()") | |
bench("Secure", "assert(SecureRandom())", "r:next_bytes()") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment