Skip to content

Instantly share code, notes, and snippets.

@akarnokd
Created Feb 14, 2014
Embed
What would you like to do?
/**
* 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 rxjava;
/**
* Benchmark utility.
*/
public final class Util {
private Util() { }
public static double measure(long count, Runnable r) {
return measure(count, false, r);
}
public static double measure(long count, boolean noprint, Runnable r) {
System.gc();
double v = 0;
long t = System.nanoTime();
try {
r.run();
} finally {
t = System.nanoTime() - t + 1;
v = count * 1E9 / t;
if (!noprint) {
System.out.printf("%,.3f ops/s%n", v);
}
}
return v;
}
public static void measureLoop(int times, long count, Runnable r) {
double sum = 0;
for (int i = 0; i < times; i++) {
System.out.printf("%d | ", i);
sum += measure(count, true, r);
}
System.out.printf("Average | %,.3f ops/s%n", sum / times);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment