Time a function: Difference between revisions

Updated to work with Nim 1.4: added missing parameter type, replaced "stmt" with "untyped", replaced "expr" with "float". Also removed "echo" in proc "doWork". Other changes.
(Added 11l)
(Updated to work with Nim 1.4: added missing parameter type, replaced "stmt" with "untyped", replaced "expr" with "float". Also removed "echo" in proc "doWork". Other changes.)
Line 1,635:
 
=={{header|Nim}}==
<lang nim>import times, osstrutils
 
proc doWork(x: int) =
var n = x
for i in 0..10000000:
n += i
echo n
template time(sstatement: stmtuntyped): exprfloat =
 
template time(s: stmt): expr =
let t0 = cpuTime()
statement
s
cpuTime() - t0
echo "Time = ", time(doWork(100)).formatFloat(ffDecimal, precision = 3), " s"</lang>
 
{{out}}
echo time(doWork(100))</lang>
Compiled in debug mode (no options).
Output:
<pre>2Time = 0.2000000000000000e-01046 s</pre>
 
=={{header|OCaml}}==
Anonymous user