nim-stint/benchmarks/bench_mod.nim

50 lines
1.0 KiB
Nim
Raw Normal View History

2018-05-08 13:51:55 +00:00
import ../stint/mpint, times
2018-03-26 10:50:50 +00:00
# Warmup on normal int
var start = cpuTime()
block:
var foo = 123
for i in 0 ..< 10_000_000:
foo += i*i mod 456
foo = foo mod 789
# Compiler shouldn't optimize away the results as cpuTime rely on sideeffects
var stop = cpuTime()
echo "Warmup: " & $(stop - start) & "s"
####################################
start = cpuTime()
block:
2018-04-25 10:52:00 +00:00
var foo = 123.u(256)
2018-03-26 10:50:50 +00:00
for i in 0 ..< 10_000_000:
2018-04-25 10:52:00 +00:00
foo += i.u(256) * i.u(256) mod 456.u(256)
foo = foo mod 789.u(256)
2018-03-26 10:50:50 +00:00
stop = cpuTime()
echo "Library: " & $(stop - start) & "s"
2018-03-26 12:46:38 +00:00
when defined(bench_ttmath):
# need C++
import ttmath
template tt_u256(a: int): UInt[256] = ttmath.u256(a.uint)
start = cpuTime()
block:
var foo = 123.tt_u256
for i in 0 ..< 10_000_000:
foo += i.tt_u256 * i.tt_u256 mod 456.tt_u256
2018-03-26 12:46:38 +00:00
foo = foo mod 789.tt_u256
stop = cpuTime()
echo "TTMath: " & $(stop - start) & "s"
2018-04-21 10:16:26 +00:00
# On my i5-5257 broadwell with the flags:
# nim c -d:release -d:bench_ttmath
# Warmup: 0.04060799999999999s
# Library: 0.9576759999999999s
# TTMath: 0.758443s