Skip to content

Instantly share code, notes, and snippets.

@timotheecour
Created December 11, 2020 08:00
Show Gist options
  • Save timotheecour/900cbb63e293d7398e2bed9293f29b46 to your computer and use it in GitHub Desktop.
Save timotheecour/900cbb63e293d7398e2bed9293f29b46 to your computer and use it in GitHub Desktop.
when true:
# benchmark
import std/threadutils
import timn/exp/once1
import std/times
var block1: Once
initOnce(block1)
template mainAux(algo) =
block:
proc bar(i: int, c: int): int =
result = i
template fn =
echo (i, c)
result += c
if c >= 1234:
when algo == "once(block1)":
once(block1): fn()
elif algo == "onceGlobal":
onceGlobal: fn()
elif algo == "onceThread":
onceThread: fn()
elif algo == "system.once":
system.once: fn()
else: static: doAssert false
proc main =
let n = 500_000_000
var c = 0
let t = cpuTime()
for i in 0..<n:
c+=bar(i, c)
let t2 = cpuTime() - t
echo (c, astToStr(algo), t2)
for j in 0..<5:
main()
mainAux("once(block1)")
mainAux("onceGlobal")
mainAux("onceThread")
mainAux("system.once")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment