Last active
August 29, 2015 14:01
-
-
Save akarnokd/4110c44e9cb62d974750 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2014 Netflix, Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |
* use this file except in compliance with the License. You may obtain a copy of | |
* the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations under | |
* the License. | |
*/ | |
package rx; | |
public final class Benchmark { | |
private Benchmark() { } | |
public static double measure(int count, int times, Runnable run) { | |
long[] ts = new long[count]; | |
for (int i = 0; i < count; i++) { | |
System.out.printf("%d | ", i + 1); | |
System.gc(); | |
long start = System.nanoTime(); | |
try { | |
run.run(); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} finally { | |
start = System.nanoTime() - start; | |
} | |
ts[i] = start; | |
} | |
long sum = 0; | |
int n = 0; | |
for (int i = Math.max(0, count - 5); i < count; i++) { | |
sum += ts[i]; | |
n++; | |
} | |
double throughput = 1E9 * n * times / sum; | |
System.out.printf("| Avg.: %,.3f ops/s, %.3f ns per item%n", throughput, sum * 1d / n / times); | |
return throughput; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment