Created
June 13, 2024 21:12
-
-
Save demotomohiro/62530b5eb31ca3d38efe541d038c9675 to your computer and use it in GitHub Desktop.
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
import std/[cmdline, times, monotimes, random, hashes] | |
template bench(body: untyped): untyped = | |
let start = getMonoTime() | |
body | |
let finish = getMonoTime() | |
echo "Time: ", (finish - start).inMicroseconds, "μ sec" | |
proc main = | |
# Use paramCount() to initialize rand so that compiler cannot run this code | |
# at compile time. | |
# And rand produce same values as long as paramCount() returns same value | |
# and this benchmark uses same data. | |
let paramC = paramCount() | |
var | |
rand = (paramC or 123).initRand() | |
testData: string | |
for i in 0 .. (100000000 + paramC): | |
testData.add rand.rand(char) | |
echo "Looong string hash bench" | |
block: | |
var h: Hash | |
bench: | |
h = hash(testData) | |
echo h | |
let numLoop = testData.len div 10 | |
echo "11 bytes" | |
block: | |
var h: Hash | |
bench: | |
for i in 0 .. numLoop: | |
h = h xor hash(testData.toOpenArrayByte(i, i + 11)) | |
echo h | |
echo "8 bytes" | |
block: | |
var h: Hash | |
bench: | |
for i in 0 .. numLoop: | |
let v = i.uint64 + paramC.uint64 | |
h = h xor hash(cast[array[8, byte]](v)) | |
echo h | |
echo "4 bytes" | |
block: | |
var h: Hash | |
bench: | |
for i in 0 .. numLoop: | |
let v = i.uint32 + paramC.uint32 | |
h = h xor hash(cast[array[4, byte]](v)) | |
echo h | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment