Skip to content

Instantly share code, notes, and snippets.

@nhojpatrick
Created September 14, 2017 21:10
Show Gist options
  • Save nhojpatrick/a22f1fc963b0c4c1d06c390f0208647b to your computer and use it in GitHub Desktop.
Save nhojpatrick/a22f1fc963b0c4c1d06c390f0208647b to your computer and use it in GitHub Desktop.
Jit Example
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();
}
}
oneDuration =62671053
twoDuration =37920883
threeDuration=47378592
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment