Skip to content

Instantly share code, notes, and snippets.

@bjpeterdelacruz
Created February 18, 2014 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjpeterdelacruz/9064842 to your computer and use it in GitHub Desktop.
Save bjpeterdelacruz/9064842 to your computer and use it in GitHub Desktop.
This code snippet shows you how to warm up the JVM before performing any benchmarks.
private static final int NUM_ITERATIONS = 100;
// ...
System.out.println("Warming up...");
for (int i = 1; i <= NUM_ITERATIONS; i++) {
for (Node node : nodes) {
Band<Float> band = createBand(Float.class);
Random random = new Random();
statsMap.put(band, new Stats(band, random.nextInt(200),
random.nextFloat() * 10));
}
Collections.sort(new ArrayList<Stats<(statsMap.values()));
statsMap.clear();
}
long totalRuntime = 0;
System.out.println("Benchmarking...");
for (int i = 1; i <= NUM_ITERATIONS; i++) {
for (Node node : nodes) {
Band<Float> band = createBand(Float.class);
Random random = new Random();
statsMap.put(band, new Stats(band, random.nextInt(200),
random.nextFloat() * 10));
}
start = System.currentTimeMillis();
Collections.sort(new ArrayList<Stats>(statsMap.values()));
end = System.currentTimeMillis();
System.out.println("Run #" + i + ": " + (end - start) + " ms");
totalRuntime += (end - start);
statsMap.clear();
}
double average = (totalRuntime / (double) NUM_ITERATIONS);
System.out.println("Average runtime: " + average + " ms");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment