-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark.nim
47 lines (36 loc) · 1.13 KB
/
benchmark.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#
# nimrod-tools
# (c) Copyright 2013 Adrian Veith
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module implements a simple benchmark template
import strutils
when not defined(JS) :
import times
proc timeStampInMS * () : float {.inline.} = cpuTime() * 1000.0
proc formatTime(val: float, prec: int): string {.inline.} = val.formatFloat(ffDecimal, prec)
else :
type
TDate {.final, importc.} = object
getTime: proc (): float
proc newDate() : TDate {.importc: "new Date", nodecl.}
proc timeStampInMS * () : float {.inline.} = newDate().getTime()
template formatTime(val: float, prec: int): string =
$val
template bench* (call: stmt): stmt =
bind timeStampInMS, formatTime
block:
let tStart = timeStampInMS()
call
let dur = timeStampInMS() - tStart
if dur > 9999.9 :
echo formatTime(dur / 1000.0, 2), " s"
else :
echo formatTime(dur, 1), " ms"
template bench* (msg: expr, call: stmt): stmt =
stdout.write msg & ": "
stdout.flushFile()
bench():
call