Home > Back-end >  Higher efficiency than the StringTokenizer?
Higher efficiency than the StringTokenizer?

Time:01-07

String TMP=new String (x);
While (true) {
String subString=null;
Int index=TMP. IndexOf (", ");
If (index & lt; 0 {
break;
}
The subString=TMP. The subString (0, index);
TMP=TMP. The substring (index + 1);
}
Saw a lot of people have said above this code efficiency is high, but after I n times test, how is lower than the split n times

CodePudding user response:

Eeee, so what's the title,

CodePudding user response:

Split string I generally use the split, mainly is convenient

CodePudding user response:

StringTokenizer efficiency is higher than the split

CodePudding user response:

refer to the second floor wowpH response:
string segmentation I generally use the split, mainly convenient

If the string is a small number of cases, the split is convenient and commonly used the StringTokenizer efficiency is high, I don't understand is why so many people said that the code above efficiency is higher than the two built-in function, the key is I measured the opposite:

CodePudding user response:

Time comparison, the self is above the time of application:
StringTokenizer: 0.019
The split: 0.012
Self: 12.549
StringTokenizer: 0.004
The split: 0.004
Self: 10.389
StringTokenizer: 0.004
The split: 0.007
Self: 10.812
StringTokenizer: 0.003
The split: 0.002
Self: 10.809
StringTokenizer: 0.003
The split: 0.002
Self: 10.64
StringTokenizer: 0.003
The split: 0.002
Self: 10.292
StringTokenizer: 0.003
The split: 0.002
Self: 10.585
Efficiency is n times slower, the above procedure is I write wrong or what

CodePudding user response:

Is my is too low,,
I saw a blog,
"Java code optimization: why do some people say that the StringTokenizer than String. The split () high efficiency? He tested?" , watch together at https://blog.csdn.net/qq_27093465/article/details/92612980? Utm_source=app

CodePudding user response:

According to his meaning, should be the split faster

CodePudding user response:

StringTokenizer said in comments, the new code is not recommended to use this class, you can use the String of the split or the Pattern of the split
Thinking in Java still use Scanner designated separator in a regular expression to scan a string

CodePudding user response:

seems indeed, the split is more efficient, lower consumption of CPU time,,, ok, pit,,,

CodePudding user response:

How could this paragraph is faster than the split, which each loop has copied the N - 1 element, a set of loop down, complexity is almost (N ^ 2/2), and the split a cycle down complexity is O (N)
  • Related