The author of the original article pointed out an effective way to improve Java performance: using Java assembly, but how to weigh the efficiency and other software performance (such as maintainability and scalability, you need to make a trade-off based on your project needs.
Oolong has been used these days
Learning Java
Assembly,
Also try to decompile some java
Code. I suddenly realized that I had made a lot of mistakes in programming in the past, resulting in low code efficiency. Some notes are listed here.
1.
Avoid using iterator whenever possible
(Not necessary)
I used to read c ++ by myself
See STL
Iterator in
The usage of iterator is good in many books.
You do not need to modify the code. I am a good reader. Since then, I like to use iterator.
For example, the following code (the first section)
For (INT I = 0; I <results. Size (); I ++ ){
String result = results. Get (I );
}
All changed to (second segment)
Iterator <string> iter =
Results. iterator ();
While (ITER. hasnext ()){
String result = ITER. Next ();
}
Oolong,
Decompile the two codes, Mom.
Java
Assembly is
. Line 11
9: iconst_0
L10: istore 5
L12: iload 5
L14: aload 4
L16: invokevirtual Java/util/arraylist/size () I
L19: if_icmpge L40
. Line 12
L22: aload 4
L24: iload 5
L26: invokevirtual Java/util/arraylist/get
(I) ljava/lang/object;
L29: checkcast Java/lang/string
L32: astore 6
. Line 11
L34: iinc 5 1
L37: goto L12
Java
Assembly is
. Line 15
L40: aload 4
L42: invokevirtual Java/util/arraylist/iterator
() Ljava/util/iterator;
L45: astore 5
. Line 16
L47: aload 5
L49: invokeinterface Java/util/iterator/hasnext () Z 1
L54: ifeq l72
. Line 17
L57: aload 5
L59: invokeinterface Java/util/iterator/next
() Ljava/lang/object; 1
L64: checkcast Java/lang/string
L67: astore 6
. Line 18
L69: goto l47
Take a closer look, the previous section uses invokevirtual.
The following section uses the invokeinterface
In principle, invokeinterface
Faster than invokevirtual
It is much slower. Test the two sections of code and find that the next section of code takes an order of magnitude longer than the previous one. Do not use iterator in the future.
Now
Second, do not use interfaces if necessary.
When I was writing a program, the superstitious type was divided, and the results were undefined, and the interface of returning objects in a clear color was returned.
For example, a method for returning intermediate results becomes like this:
Public list <string> getlist (){
List <string> Results = new arraylist <string> ();
Return results;
}
The processing method is as follows:
Public void handle (list <string> contents ){
For (INT Index = 0; index <contents. Size (); index ++ ){
String content = contents. Get (INDEX );
}
}
Decompile handle
Found
. Line 9
L0: iconst_0
L1: istore_2
L2: iload_2
L3: aload_1
L4: invokeinterface Java/util/LIST/size () I 1
A9: if_icmpge l29
. Line 10
L12: aload_1
L13: iload_2
L14: invokeinterface Java/util/LIST/get (I) ljava/lang/object;
2
L19: checkcast Java/lang/string
L22: astore_3
. Line 9
L23: iinc 2 1
L26: goto L2
Again, invokeinterface
, Change it back.
Public void handle (arraylist <string>
Contents ){
For (INT Index = 0; index <contents. Size (); index ++ ){
String content = contents. Get (INDEX );
}
}
Public arraylist <string> getlist (){
Arraylist <string> Results = new arraylist <string> ();
Return results;
}
This will be good, not invokeinterface
Is invokevirtual
Fast
. Line 9
L0: iconst_0
L1: istore_2
L2: iload_2
L3: aload_1
L4: invokevirtual Java/util/arraylist/size () I
L7: if_icmpge L25
. Line 10
L10: aload_1
L11: iload_2
L12: invokevirtual Java/util/arraylist/get (I) ljava/lang/object;
L15: checkcast Java/lang/string
L18: astore_3
. Line 9
L19: iinc 2 1
L22: goto L2
3. 1.5
New features,
Use with caution
See 1.5
New features, easy to use, this
For (string content: Contents ){
}
Decompilation, how is it still iterator ?,
Be careful,
Don't try it out.
L0: aload_1
L1: invokevirtual Java/util/arraylist/iterator
() Ljava/util/iterator;
L4: astore_2
L5: aload_2
L6: invokeinterface Java/util/iterator/hasnext () Z 1
L11: ifeq L27
L14: aload_2
L15: invokeinterface Java/util/iterator/next
() Ljava/lang/object; 1
L20: checkcast Java/lang/string
L23: astore_3
. Line 10
L24: goto L5
4. 1.5
String
No longer a terrible performance bottleneck
In objective Java
I once said that we should use strings as few as possible
For connection operations, stringbuffer
Better performance, can reduce new string
Number of times
But in 1.5
Try the following code
String contents = "";
For (INT I = 0; I <10000; I ++)
Contents + = "hello ";
The Decompilation results show that this is not the case at all.
. Line 9
L0: LDC ""
L2: astore_1
. Line 10
L3: iconst_0
L4: istore_2
L5: iload_2
L6: sipush 10000
A9: if_icmpge l38
. Line 11
L12: New Java/lang/stringbuilder
L15: DUP
L16: invokespecial Java/lang/stringbuilder/<init> () V
L19: aload_1
L20: invokevirtual Java/lang/stringbuilder/append
(Ljava/lang/string;) ljava/lang/stringbuilder;
L23: LDC "hello"
L25: invokevirtual Java/lang/stringbuilder/append
(Ljava/lang/string;) ljava/lang/stringbuilder;
L28: invokevirtual Java/lang/stringbuilder/tostring
() Ljava/lang/string;
L31: astore_1
. Line 10
L32: iinc 2 1
L35: goto L5
Stringbuffer is used here.
Stringbuilder,
Of course, it may be IBM
Java
Compiler optimization, or 1.5
The compiler has been optimized (confirmed in the future ).
String compiled on
The connection code is completely efficient, at least I can safely use it now