During performance optimization, JProfiler was used to test the performance of Web applications. It was found that a replaceBlank function took 10% of the CPU time, it is a simple function to remove blank strings in XML documents using regular expressions. However, this simple function consumes 10% of the performance.
In Web applications, removing the blank string seems to be a simple function, but it is also a bit troublesome to write. Summary.
Method 1: Regular Expression
There are two writing methods:
s.replaceAll(\s+, );s.replaceAll(\s, );
Which of the following statements is better? It depends on the specific scenario. Each type of consecutive blank string is selected. If only one blank string exists, the second type is selected. Individuals tend to be the first type.
Regular Expressions are relatively slow, which is 3 to 4 times slower than the following method.
Method 2: org. springframework. util. StringUtils. trimAllWhitespace
The specific implementation code is as follows:
public static String trimAllWhitespace(String str) {if (!hasLength(str)) {return str;}StringBuilder sb = new StringBuilder(str);int index = 0;while (sb.length() > index) {if (Character.isWhitespace(sb.charAt(index))) {sb.deleteCharAt(index);}else {index++;}}return sb.toString();}
It seems that there is no problem, but the programmer's intuition is: how is the deleteCharAt function implemented? There should be no efficient algorithms to achieve this.
The implementation code is as follows:
public AbstractStringBuilder deleteCharAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); System.arraycopy(value, index+1, value, index, count-index-1); count--; return this; }Obviously, too many calls to System. arraycopy may cause performance problems.
Method 3: Call the StringBuilder. append function.
static public String myTrimAllWhitespace(String str) {if (str != null) {int len = str.length();if (len > 0) {StringBuilder sb = new StringBuilder(len);for (int i = 0; i < len; ++i) {char c = str.charAt(i);if (!Character.isWhitespace(c)) {sb.append(c);}}return sb.toString();}}return str;}This is the initial idea. In actual tests, it is found that the efficiency in most cases is higher than that in method 2.
However, in some cases, such as aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaa, this type only contains one blank character, and the efficiency is slow.
Method 4: Use System. arraycopy to copy part of the memory.
Method 2: When deleteAt is called, all strings following the entire copy are required. Obviously, the efficiency will decrease when the string is very long. Therefore, only part of the memory is copied.
Two pos types are used to mark which part is a continuous non-blank string.
static public String myTrimAllWhitespace3(String str) {if (str != null) {int len = str.length();if (len > 0) {char[] src = str.toCharArray();char[] dest = new char[src.length];int destPos = 0;for (int pos1 = 0, pos2 = 0; pos2 < src.length;) {if (Character.isWhitespace(src[pos2])) {if (pos1 == pos2) {pos1++;pos2++;} else {System.arraycopy(src, pos1, dest, destPos, pos2- pos1);destPos += (pos2 - pos1);pos2++;pos1 = pos2;}} else {pos2++;}if (pos2 == src.length) {if (pos1 != pos2) {System.arraycopy(src, pos1, dest, destPos, pos2- pos1);destPos += (pos2 - pos1);}return new String(dest, 0, destPos);}}}}return str;}
Method 5: Remove StringBuilder and directly operate char []
After writing method 4, the test finds that the efficiency is in the middle, and the comparison with method 2 and three-phase is not good or bad. It seems that a balance point has been found.
However, I suddenly thought that since Method 4 does not directly operate the char [] array, why not do the same in method 2? So there is:
static public String myTrimAllWhitespace2(String str) {if (str != null) {int len = str.length();if (len > 0) {char[] dest = new char[len];int destPos = 0;for (int i = 0; i < len; ++i) {char c = str.charAt(i);if (!Character.isWhitespace(c)) {dest[destPos++] = c;}}return new String(dest, 0, destPos);}}return str;}
Point 6: Unicode
The preceding methods can only handle most of the cases. For some Unicode strings, this may cause problems.
Because I am sensitive to this, I finally write a Unicode Character for processing:
static public String myTrimAllWhitespace3(String str) {if (str != null) {int len = str.length();if (len > 0) {char[] src = str.toCharArray();char[] dest = new char[src.length];int destPos = 0;for (int pos1 = 0, pos2 = 0; pos2 < src.length;) {if (Character.isWhitespace(src[pos2])) {if (pos1 == pos2) {pos1++;pos2++;} else {System.arraycopy(src, pos1, dest, destPos, pos2- pos1);destPos += (pos2 - pos1);pos2++;pos1 = pos2;}} else {pos2++;}if (pos2 == src.length) {if (pos1 != pos2) {System.arraycopy(src, pos1, dest, destPos, pos2- pos1);destPos += (pos2 - pos1);}return new String(dest, 0, destPos);}}}}return str;}This process of Unicode is very slow .. Java's String class does not expose Enough functions to process Unicode.
Summary:
The test code is as follows:
Https://gist.github.com/hengyunabc/a4651e90db24cc5ed29a
The fastest way to test code on my computer is in five.
In some special cases, using System. arraycopy in Method 4 to copy and Mark Two segments of memory will be faster, but this algorithm is too complicated and worth the candle.
I prefer an intuitive and efficient linear algorithm.
A path is provided to Spring. Method 3 code is used at the beginning. However, in some cases, the efficiency is not high, which leads to a restless weekend .. So there are several methods.
It is not easy to implement a simple function directly, so I try to avoid writing Util classes and methods, because it is not easy to ensure the quality and performance of the Code.
Https://github.com/spring-projects/spring-framework/pull/562