Created
September 14, 2017 21:10
-
-
Save nhojpatrick/a22f1fc963b0c4c1d06c390f0208647b to your computer and use it in GitHub Desktop.
Jit Example
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 JitExample { | |
public static void main(final String[] args) { | |
new JitExample(); | |
} | |
public JitExample() { | |
final long oneStart = System.nanoTime(); | |
for (int i = 0; i < 1000; i++) { | |
final StringBuffer sb = new StringBuffer(); | |
for (int j = 0; j < i; j++) { | |
sb.append(j); | |
} | |
System.out.println(sb.toString().length()); | |
} | |
final long oneDuration = System.nanoTime() - oneStart; | |
final long twoStart = System.nanoTime(); | |
for (int i = 0; i < 1000; i++) { | |
System.out.println(calculate(i)); | |
} | |
final long twoDuration = System.nanoTime() - twoStart; | |
final long threeStart = System.nanoTime(); | |
for (int i = 0; i < 1000; i++) { | |
final StringBuffer sb = new StringBuffer(); | |
for (int j = 0; j < i; j++) { | |
sb.append(j); | |
} | |
System.out.println(sb.toString().length()); | |
} | |
final long threeDuration = System.nanoTime() - threeStart; | |
System.out.println("oneDuration =" + oneDuration); | |
System.out.println("twoDuration =" + twoDuration); | |
System.out.println("threeDuration=" + threeDuration); | |
} | |
public int calculate(int i) { | |
final StringBuffer sb = new StringBuffer(); | |
for (int j = 0; j < i; j++) { | |
sb.append(j); | |
} | |
return sb.toString().length(); | |
} | |
} |
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
oneDuration =62671053 | |
twoDuration =37920883 | |
threeDuration=47378592 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment