Skip to content

Instantly share code, notes, and snippets.

@pron
Created May 5, 2014 17:56
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 pron/dbddbf6da00f346716e6 to your computer and use it in GitHub Desktop.
Save pron/dbddbf6da00f346716e6 to your computer and use it in GitHub Desktop.
public class Bench {
public static void main(String[] args) {
double res;
long start;
int numVals = 1000000;
System.out.println("Warming up");
start = System.nanoTime();
res = bar(new double[10000], 5000);
System.out.println("Done. " + ((System.nanoTime() - start) / 1000000) + " ms (" + res + ")");
System.out.println("Starting benchmark");
start = System.nanoTime();
res = bar(new double[numVals], 10000);
System.out.println("Done. " + ((System.nanoTime() - start) / 1000000) + " ms (" + res + ")");
}
static double bar(double[] vals, int repeats) {
for (int i = 0; i < repeats; i++)
foo(vals);
return vals[vals.length - 1]; // don't optimize away
}
static void foo(double[] vals) {
for (int i = 0; i < vals.length; i++) {
double x = i;
vals[i] = Math.sqrt(x * x + 123.4);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment