Last active
July 5, 2018 11:48
-
-
Save rozag/f5d7ba6e928b621604fcaf2cee160bcd to your computer and use it in GitHub Desktop.
A dummy benchmark helper
This file contains hidden or 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
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public final class BenchmarkUtil { | |
private static final List<Long> TIMES = new ArrayList<>(10); | |
private static long startNanos; | |
public static void started() { | |
startNanos = System.nanoTime(); | |
} | |
public static void ended() { | |
final long endNanos = System.nanoTime(); | |
final long elapsedNanos = endNanos - startNanos; | |
TIMES.add(elapsedNanos); | |
final int count = TIMES.size(); | |
long sum = 0; | |
for (int i = 0; i < count; i++) { | |
sum += TIMES.get(i); | |
} | |
final long mean = sum / count; | |
Collections.sort(TIMES); | |
final long median; | |
final int middle = count / 2; | |
if (count % 2 == 1) { | |
median = TIMES.get(middle); | |
} else { | |
median = (TIMES.get(middle - 1) + TIMES.get(middle)) / 2; | |
} | |
Log.d("TIME", "elapsed=" + elapsedNanos + "; mean=" + mean + "; median=" + median + "; count=" + count); | |
} | |
private BenchmarkUtil() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment