Last active
December 6, 2018 08:01
-
-
Save technicallyagd/b4bce3d5932ba43ee28ab0a324c9cf4a to your computer and use it in GitHub Desktop.
Minimal example to demonstrate performance difference between seq and array access
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 times | |
template benchmark*(benchmarkName: string, code: untyped) = | |
block: | |
let timeStart = cpuTime() | |
code | |
echo benchmarkName | |
echo cpuTime() - timeStart, "s" | |
var seqBase = @[@[1, 2], @[322146413, 397146891], @[2100521352, 391739920], @[ | |
330559900, 77684297], @[908332622, 428261606], @[1, 2], @[322146413, | |
397146891], @[2100521352, 391739920], @[ | |
330559900, 77684297], @[908332622, 428261606]] | |
var arrayBase = @[[1, 2], [322146413, 397146891], [2100521352, 391739920], [ | |
330559900, 77684297], [908332622, 428261606], [1, 2], [322146413, | |
397146891], [2100521352, 391739920], [ | |
330559900, 77684297], [908332622, 428261606]] | |
var | |
someSeqOSeq = seqBase | |
someSeqOArray = arrayBase | |
const | |
nIter = 200 | |
nCopy = 100 | |
for i in 0..nCopy: | |
someSeqOSeq &= seqBase | |
someSeqOArray &= arrayBase | |
benchmark "Seq of Seq": | |
var count = 0 | |
for x in 0..nIter: | |
for y in 0..nIter: | |
for s in someSeqOSeq: | |
let a = s[0] | |
let b = s[1] | |
if ((a + b) * 23) mod b == 0: count += 1 | |
benchmark "Seq of Array": | |
var count = 0 | |
for x in 0..nIter: | |
for y in 0..nIter: | |
for s in someSeqOArray: | |
let a = s[0] | |
let b = s[1] | |
if ((a + b) * 23) mod b == 0: count += 1 |
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 times | |
template benchmark*(benchmarkName: string, code: untyped) = | |
block: | |
let timeStart = cpuTime() | |
code | |
echo benchmarkName | |
echo cpuTime() - timeStart, "s" | |
let someSeqOSeq = @[@[1, 2], @[322146413, 397146891], @[2100521352, 391739920], @[ | |
330559900, 77684297], @[908332622, 428261606], @[1, 2], @[322146413, | |
397146891], @[2100521352, 391739920], @[ | |
330559900, 77684297], @[908332622, 428261606]] | |
let someSeqOArray = @[[1, 2], [322146413, 397146891], [2100521352, 391739920], [ | |
330559900, 77684297], [908332622, 428261606], [1, 2], [322146413, | |
397146891], [2100521352, 391739920], [ | |
330559900, 77684297], [908332622, 428261606]] | |
const nIter = 1000 | |
benchmark "Seq of Seq": | |
var count = 0 | |
for x in 0..nIter: | |
for y in 0..nIter: | |
for s in someSeqOSeq: | |
let a = s[0] | |
let b = s[1] | |
if ((a + b) * 23) mod b == 0: count += 1 | |
benchmark "Seq of Array": | |
var count = 0 | |
for x in 0..nIter: | |
for y in 0..nIter: | |
for s in someSeqOArray: | |
let a = s[0] | |
let b = s[1] | |
if ((a + b) * 23) mod b == 0: count += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using compiler built from commit 9eaf99f7e61d07f5502b3656a255ea944c47f984.
Result of
main.nim
from my machine:-d:release
:Result of
longOuter.nim
from my machine:-d:release
: