Last active
January 8, 2020 23:18
-
-
Save brentp/dabc239aad992ccc9bc12ccb20451951 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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