Created
May 5, 2014 17:56
-
-
Save pron/dbddbf6da00f346716e6 to your computer and use it in GitHub Desktop.
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
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