Skip to content

Instantly share code, notes, and snippets.

@killme2008
Created February 18, 2017 07:29
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 killme2008/efa50857e1e66223b481dc120d80c6bc to your computer and use it in GitHub Desktop.
Save killme2008/efa50857e1e66223b481dc120d80c6bc to your computer and use it in GitHub Desktop.
Test
public class Test {
private static int plusOne(int i) {
return ++i;
}
private static long oneForMethod(int x, int times) {
long start = System.nanoTime();
for (int i = 0; i < times; i++) {
x = plusOne(x);
x = plusOne(x);
x = plusOne(x);
}
long end = System.nanoTime();
return end - start;
}
private static long threeForMethod(int x, int times) {
long start = System.nanoTime();
for (int i = 0; i < times; i++) {
x = plusOne(x);
}
for (int i = 0; i < times; i++) {
x = plusOne(x);
}
for (int i = 0; i < times; i++) {
x = plusOne(x);
}
long end = System.nanoTime();
return end - start;
}
public static void main(String[] args) {
test(false);
test(false);
test(false);
}
private static void test(boolean warmup) {
long[] result = new long[6];
int loops = 10000;
int x = 0;
for (int i = 0; i < loops; i++) {
result[0] += threeForMethod(x, 10000);
result[1] += oneForMethod(x, 10000);
}
if (!warmup) {
System.out.println(result[0]);
System.out.println(result[1]);
}
}
}
@killme2008
Copy link
Author

加上参数 -XX:+PrintCompilation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment