Created
February 18, 2017 07:29
-
-
Save killme2008/efa50857e1e66223b481dc120d80c6bc to your computer and use it in GitHub Desktop.
Test
This file contains 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 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]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
加上参数 -XX:+PrintCompilation