Skip to content

Instantly share code, notes, and snippets.

@technicallyagd
Last active December 6, 2018 08:01
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 technicallyagd/b4bce3d5932ba43ee28ab0a324c9cf4a to your computer and use it in GitHub Desktop.
Save technicallyagd/b4bce3d5932ba43ee28ab0a324c9cf4a to your computer and use it in GitHub Desktop.
Minimal example to demonstrate performance difference between seq and array access
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
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
@technicallyagd
Copy link
Author

technicallyagd commented Dec 6, 2018

using compiler built from commit 9eaf99f7e61d07f5502b3656a255ea944c47f984.
Result of main.nim from my machine:

  • with -d:release:
Seq of Seq
0.45509076s
Seq of Array
0.1059315100000001s
  • without:
Seq of Seq
4.405831273s
Seq of Array
0.4498233489999999s

Result of longOuter.nim from my machine:

  • with -d:release:
Seq of Seq
1.941198598s
Seq of Array
0.3409378690000002s
  • without:
Seq of Seq
18.001304946s
Seq of Array
1.463049079000001s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment