Skip to content

Instantly share code, notes, and snippets.

@brentp
Last active January 8, 2020 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentp/dabc239aad992ccc9bc12ccb20451951 to your computer and use it in GitHub Desktop.
Save brentp/dabc239aad992ccc9bc12ccb20451951 to your computer and use it in GitHub Desktop.
import times
type AThing = ref object
i: int
iterator items(a:AThing): int =
when defined(dodefer):
defer:
a.i = 11
else:
a.i = 11
for i in 0..a.i:
yield i
proc speed(n:int): int =
var j:int
var a = AThing(i:10)
var l = 0
for k in 0..<n:
j = 0
for i in a:
j += 1
if i > 5: break
l += 1
doAssert j > 0
doAssert a.i == 11
echo l
return j
import times
when defined(dodefer):
echo "using defer"
else:
echo "no defer compile with -d:dodefer to check speed"
const N = 50000000
var t = cpuTime()
echo speed(N)
echo cpuTime() - t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment