Rate counter: Difference between revisions

(added Arturo)
Line 1,181:
return times;
}</syntaxhighlight>
 
=={{header|jq}}==
'''Works with jq and gojq, that is, the C and Go implementations of jq.'''
 
In this entry, the times to compute `x*x*x` are compared with the times to compute `pow(x;3)`.
 
Note that jq only has direct access to the system clock ("now").
<syntaxhighlight lang=jq>
def cube: . * . * .;
 
def pow3: pow(.; 3);
 
def benchmark($n; func; $arg; $calls):
reduce range(0; $n) as $i ([];
now as $_
| (range(0; $calls) | ($arg | func)) as $x
# milliseconds:
| . + [(now - $_) * 1000 | floor] ) ;
 
"Timings (total elapsed time in milliseconds):",
"cube pow3",
([benchmark(10; cube; 5; 1e5), benchmark(10; pow3; 5; 1e5)]
| transpose[]
| "\(.[0]) \(.[1])" )
</syntaxhighlight>
'''Invocation''': jq -nr -f rate-counter.jq
{{{output}}
<pre>
Timings (total elapsed time in milliseconds):
cube pow3
205 178
164 182
182 156
173 173
167 154
181 175
180 176
176 160
186 206
173 174
</pre>
 
 
=={{header|Jsish}}==
2,442

edits