Skip to content

Instantly share code, notes, and snippets.

@appgurueu
Created July 19, 2024 23:55
Show Gist options
  • Save appgurueu/0fcbe99941064b639c014c2d43318730 to your computer and use it in GitHub Desktop.
Save appgurueu/0fcbe99941064b639c014c2d43318730 to your computer and use it in GitHub Desktop.
Minetest random benchmark
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