Rate counter: Difference between revisions

Content added Content deleted
(→‎{{header|Java}}: include non-stream version)
(→‎{{header|XPL0}}: added zkl)
Line 1,649: Line 1,649:
1424391 microseconds for 100 samples = 70.20544Hz
1424391 microseconds for 100 samples = 70.20544Hz
1395069 microseconds for 100 samples = 71.68104Hz
1395069 microseconds for 100 samples = 71.68104Hz
</pre>


=={{header|zkl}}==
One second resolution and we let the computation run to completion. We could run the computation in a thread and signal it if time is up but that doesn't seem reasonable.
<lang zkl>fcn rateCounter(f,timeNRuns,secsToRun=Void){
now:=Time.Clock.time;
if(secsToRun){
then:=now + secsToRun;
N:=0; do{ f(); N+=1; }while(Time.Clock.time<then);
t:=Time.Clock.time - now;
println("%d runs in %s seconds = %.3f sec/run"
.fmt(N,Time.Date.toHMSString(0,0,t),t.toFloat()/N));
}
else{
do(timeNRuns){ f() }
t:=Time.Clock.time - now;
println("%s seconds to run %d times = %.3f sec/run"
.fmt(Time.Date.toHMSString(0,0,t),timeNRuns,
t.toFloat()/timeNRuns));
t
}
}</lang>
<lang zkl>ns:=List.createLong(0d100_000,(0).random,True); // one hundred thousand ints
rateCounter('wrap(){ ns.copy().sort() },20);
rateCounter('wrap(){ ns.copy().sort() },Void,10);</lang>
{{out}}
<pre>
00:00:19 seconds to run 20 times = 0.950 sec/run
11 runs in 00:00:10 seconds = 0.909 sec/run
</pre>
</pre>