Time a function: Difference between revisions

→‎{{header|Wren}}: Now uses Benchmark class.
(Added solution for Action!)
(→‎{{header|Wren}}: Now uses Benchmark class.)
Line 2,636:
 
=={{header|Wren}}==
{{libheader|Wren-check}}
The only way Wren currently has to time a function (encapsulated in the ''Benchmark.run'' method) is to measure the System time before and after the function is called. We therefore use that approach, averaging over say 100 runs, having first shut down as many other processes as we can.
<lang ecmascript>var f = Fn.new {
<lang ecmascript>import "./check" for Benchmark
for (i in 0..1e7) {}
}
 
Benchmark.run("a function", 100, true) {
var runs = 100
for (i in 0..1e7) {}
var total = 0
}</lang>
for (i in 1..runs) {
var start = System.clock
f.call()
total = total + System.clock - start
}
System.print("Over %(runs) runs, took an average of %(total/runs) seconds.")</lang>
 
{{out}}
<pre>
Running 'a function' over 100 iteration(s):
Over 100 runs, took an average of 0.19607459 seconds.
-------------------------------------------
Best 191.172 ms
Mean 201.942 ms
Worst 208.049 ms
</pre>
 
9,485

edits