Skip to content

Instantly share code, notes, and snippets.

@lsamayoa
Last active August 29, 2015 14:24
Show Gist options
  • Save lsamayoa/c7252aa4163612c3aac2 to your computer and use it in GitHub Desktop.
Save lsamayoa/c7252aa4163612c3aac2 to your computer and use it in GitHub Desktop.
Var declaration inside or outside loop comparision
// javap -Xbatch \
// -XX:-TieredCompilation \
// -XX:+PrintCompilation \
// Main \
// 2> /dev/null
// 71 1 b java.lang.String::hashCode (55 bytes)
// 80 2 b java.lang.String::indexOf (70 bytes)
// 94 3 b sun.nio.cs.UTF_8$Encoder::encode (359 bytes)
// 98 4 b java.io.BufferedInputStream::getBufIfOpen (21 bytes)
// 99 5 s b java.io.BufferedInputStream::read (49 bytes)
// 102 6 b java.io.DataInputStream::readChar (40 bytes)
// 105 7 % b sun.text.normalizer.NormalizerDataReader::read @ 11 (83 bytes)
// 107 7 % sun.text.normalizer.NormalizerDataReader::read @ -2 (83 bytes) made not entrant
// 108 6 java.io.DataInputStream::readChar (40 bytes) made not entrant
// 109 8 s b java.io.ByteArrayInputStream::read (36 bytes)
// 112 9 b java.io.DataInputStream::readInt (72 bytes)
// 117 10 b java.io.DataInputStream::readChar (40 bytes)
// javap -Xbatch \
// -XX:-TieredCompilation \
// -XX:+PrintCompilation \
// MainInternal \
// 2> /dev/null
// 68 1 b java.lang.String::hashCode (55 bytes)
// 76 2 b java.lang.String::indexOf (70 bytes)
// 85 3 b sun.nio.cs.UTF_8$Encoder::encode (359 bytes)
// 90 4 b java.io.BufferedInputStream::getBufIfOpen (21 bytes)
// 91 5 s b java.io.BufferedInputStream::read (49 bytes)
// 94 6 b java.io.DataInputStream::readChar (40 bytes)
// 97 7 % b sun.text.normalizer.NormalizerDataReader::read @ 11 (83 bytes)
// 100 7 % sun.text.normalizer.NormalizerDataReader::read @ -2 (83 bytes) made not entrant
// 100 6 java.io.DataInputStream::readChar (40 bytes) made not entrant
// 102 8 s b java.io.ByteArrayInputStream::read (36 bytes)
// 105 9 b java.io.DataInputStream::readInt (72 bytes)
// 110 10 b java.io.DataInputStream::readChar (40 bytes)
public class Main {
public static void main(String[] args){
String variable;
for (int i = 0; i < 10000; i++) {
// We use new constructor to get a new instance each time
variable = new String("Hello");
System.err.println(variable);
}
}
}
public class MainInternal {
public static void main(String[] args){
for (int i = 0; i < 10000; i++) {
// We use new constructor to get a new instance each time
String variable = new String("Hello");
System.err.println(variable);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment