# Copyright (c) 2019-2021 Status Research & Development GmbH # Licensed and distributed under either of # * MIT license: http://opensource.org/licenses/MIT # * Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0 # at your option. This file may not be copied, modified, or distributed except according to those terms. # taken from https://github.com/status-im/nim-beacon-chain/tree/master/benchmarks import times, stats, strformat export times, stats, strformat proc warmup*() = # Warmup - make sure cpu is on max perf let start = cpuTime() var foo = 123'i64 for i in 0'i64 ..< 300_000_000'i64: foo += i*i mod 456 foo = foo mod 789 # Compiler shouldn't optimize away the results as cpuTime rely on sideeffects let stop = cpuTime() echo &"Warmup: {stop - start:>4.4f} s, result {foo} (displayed to avoid compiler optimizing warmup away)" template printStats*(experiment_name: string, compute_result: typed) {.dirty.} = echo "#################################################################" echo "\n" & experiment_name echo &"Collected {stats.n} samples in {global_stop - global_start:>4.3f} seconds" echo &"Average time: {stats.mean * 1000 :>4.3f} ms" echo &"Stddev time: {stats.standardDeviationS * 1000 :>4.3f} ms" echo &"Min time: {stats.min * 1000 :>4.3f} ms" echo &"Max time: {stats.max * 1000 :>4.3f} ms" echo "\nDisplay computation result to make sure it's not optimized away" echo compute_result # Prevents compiler from optimizing stuff away echo '\n' template bench*(name: string, compute_result: typed, body: untyped) {.dirty.}= block: # Actual bench var stats: RunningStat let global_start = cpuTime() for _ in 0 ..< nb_samples: let start = cpuTime() block: body let stop = cpuTime() stats.push stop - start let global_stop = cpuTime() printStats(name, compute_result)