> Is there a nim equivalent to Python's timeit module for timing snippets?

There are probably about 100 variants in people's local code, but I happened to 
just see your message and also to just add this to `cligen/osUt` the other day 
(needs `requires "cligen#head"` in your `.nimble` file or you could just copy 
this tiny impl and adapt as you like): 
    
    
    import strutils, times
    
    template timeIt*(label:string, unit:float, places=3, sep="\n", body: 
untyped) =
      let t0 = epochTime()
      body
      let dt = epochTime() - t0
      stdout.write label, " ", formatFloat(dt * unit, ffDecimal, places), sep
    
    timeIt("write1", 1e6, 3, " ") : stderr.write "hi"
    timeIt("write2", 1e6, 3, "\n"): stderr.write "there"
    
    
    Run

You may want to add loops to repeat and use the `stats.RunningStat` stdlib API 
to do something nice more like Python's version. There is also this package 
called golden 
([https://github.com/disruptek/golden](https://github.com/disruptek/golden)) to 
try to iterate until timing noise has been suppressed. And probably other 
various solutions, too. (And sometimes noise **is** the data and you want 
cold-cache/system competition effects included...benchmarking is obviously a 
larger topic than a snippet thing.)

Reply via email to