If you plan on doing a lot of measurements, the best approach is to create a reusable helper template that abstracts the synchronization code:
import times, os, strutils template benchmark(benchmarkName: string, code: stmt) = let t0 = epochTime() code let elapsed = epochTime() - t0 let elapsedStr = elapsed.formatFloat(format = ffDecimal, precision = 3) echo "CPU Time [", benchmarkName, "] ", elapsedStr, "s" benchmark "my benchmark": sleep 300
Will open
CPU Time [my benchmark] 0.305s
If you need more complete performance data for all the code included in your project, Nim offers a special build mode that allows you to compile code using profiling probes. You can learn more about this here:
http://nim-lang.org/docs/estp.html
Finally, since Nim generates C code with names of C functions that directly correspond to their Nim counterparts, you can use any C profiler with Nim programs.
source share