i first encountered java in 1995. that language soon became my favorite because of its elegance, its interesting language features (e.g., interfaces, garbage collector, and threads), and its similarity to c and c++ (i come from a c/c++ background).
lesson 1: the poor performance of the string concatenation operator 【程序编程相关:hibernate的hello word】this article shares with you some of the lessons ive learned while working with java. several lessons point out problem areas to avoid; all lessons offer advice on writing better code. i hope youll come away from this article with a greater understanding of the java language, an awareness of these problem areas, and stronger java coding skills. 【推荐阅读:了解 JAVA classloader】
string s = ""; for (int i = 32; i < 128; i++) s = s + (char) i;the code fragment employs a loop to initialize a string to those characters with unicode values ranging from 32 to 127 inclusive. each loop iteration uses the string concatenation and cast operators to append the next character to the string. using the string concatenation operator in a loop to build a string is often seen in c++. as with c++, the single loop looks ok from a performance perspective: it seems to ensure that the runtime performance is linear. 【扩展信息:CLR和JRE的运行机制的初步总结】
java programs should perform as well as is possible. but achieving that goal is not always easy, especially if your source codes contain frequent occurrences of javas string concatenation operator. concatenating strings via javas string concatenation operator, especially within loops, can significantly impact the performance of your programs. check out the following code fragment:
or is that performance linear? before that question can be answered, we must examine the equivalent sequence of jvm bytecodes. that sequence appears below:
0 ldc "" 2 astore_1 3 bipush 32 5 istore_2 6 iload_2 7 sipush 128 10 if_icmpge 39 13 new java/lang/stringbuilder 16 dup 17 invokespecial java/lang/stringbuilder/<init>()v 20 aload_1 21 invokevirtual java/lang/stringbuilder/append(ljava/lang/string;) ljava/lang/stringbuilder; 24 iload_2 25 i2c 26 invokevirtual java/lang/stringbuilder/append(c) ljava/lang/stringbuilder; 29 invokevirtual java/lang/stringbuilder/tostring() ljava/lang/string; 32 astore_1 33 iinc 2 1 36 goto 6 39 ...the instructions at offsets 0 and 2 correspond to string s = "";. the for loop begins at offset 3 and continues through offset 36. each for loop iteration creates a stringbuilder object and invokes two of that objects append() methods, to first append the characters in the string to the stringbuilder and then to append the next character to the stringbuilder. finally, the tostring() method is called to convert the stringbuilder back to a string. the following code fragment shows what these instructions look like from a source code perspective:
string s = ""; for (int i = 32; i < 128; i++) s = new stringbuilder ().append (s) .append ((char) i).tostring ();the code fragment above is what is really being generated when you employ the string concatenation operator. each loop iteration creates a throwaway stringbuilder object and explicitly invokes three of that objects methods. the tostring() method call creates a new string object to hold the result. the original string object is not used because string is immutable.
... 下一页