Rate counter: Difference between revisions
Content added Content deleted
(Major refactoring done to exhibit java.util.stream usage) |
(→{{header|Java}}: include non-stream version) |
||
Line 658: | Line 658: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|JavaScript}} |
|||
{{works with|Java|8}} |
|||
<lang java>import java.util.function.Consumer; |
|||
public class RateCounter { |
|||
public static void main(String[] args) { |
|||
for (double d : benchmark(10, x -> System.out.print(""), 10)) |
|||
System.out.println(d); |
|||
} |
|||
static double[] benchmark(int n, Consumer<Integer> f, int arg) { |
|||
double[] timings = new double[n]; |
|||
for (int i = 0; i < n; i++) { |
|||
long time = System.nanoTime(); |
|||
f.accept(arg); |
|||
timings[i] = System.nanoTime() - time; |
|||
} |
|||
return timings; |
|||
} |
|||
}</lang> |
|||
<pre>70469.0 |
|||
2047.0 |
|||
1169.0 |
|||
877.0 |
|||
877.0 |
|||
877.0 |
|||
877.0 |
|||
877.0 |
|||
877.0 |
|||
877.0</pre> |
|||
=== Stream based solution === |
|||
{{trans|JavaScript}} |
{{trans|JavaScript}} |
||
{{works with|Java|8}} |
{{works with|Java|8}} |