Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,35 @@ run-d: d
analyze-d:
@echo "Max Pause: "
@cat d.log

## Nim

nim: main.nim
nim c -d:release -d:disableMS -d:useRealtimeGC main.nim

clean::
rm -f main
rm -r nimcache

run-nim: nim
./main > nim.log
cat nim.log

analyze-nim:
@echo "Worst pause: "
@cat nim.log

nim-ms: main.nim
nim c -d:release -d:useRealtimeGC main.nim

clean::
rm -f main
rm -r nimcache

run-nim-ms: nim
./main > nim-ms.log
cat nim-ms.log

analyze-nim-ms:
@echo "Worst pause: "
@cat nim-ms.log
59 changes: 59 additions & 0 deletions main.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Compile and run with 'nim c -r -d:useRealtimeGC -d:release main.nim'
# See here for more info: http://forum.nim-lang.org/t/2646

import strutils

include "$lib/system/timers"

const
windowSize = 200000
msgCount = 1000000

type
Msg = seq[byte]
Buffer = seq[Msg]

var worst: Nanos

proc mkMessage(n: int): Msg =
result = newSeq[byte](1024)
for i in 0 .. <result.len:
result[i] = byte(n)

proc pushMsg0(b: var Buffer, highID: int) =
# warmup:
let m = mkMessage(highID)
shallowCopy(b[highID mod windowSize], m)

proc pushMsg1(b: var Buffer, highID: int) =
# with benchmarking:
let start = getTicks()

let m = mkMessage(highID)
shallowCopy(b[highID mod windowSize], m)

let elapsed = getTicks() - start
if elapsed > worst:
worst = elapsed

proc main() =
# Don't use GC_disable() and GC_step(). Instead use GC_setMaxPause().
when defined(disableMS):
GC_disableMarkAndSweep()
GC_setMaxPause(300)

var b = newSeq[Msg](windowSize)
# we need to warmup Nim's memory allocator so that not most
# of the time is spent in mmap()... Hopefully later versions of Nim
# will be smarter and allocate larger pieces from the OS:
for i in 0 .. <msgCount:
pushMsg0(b, i)

# now after warmup, we can measure things:
for i in 0 .. <msgCount:
pushMsg1(b, i)

echo(worst.float / 1_000_000.0, " ms")

when isMainModule:
main()