An article by the brother of inspiration yesterday, which carefully compared the problem of ignoring string case-sensitivity replacement in. net. Five existing solutions and detailed efficiency test data are provided. However, after careful reading, I am not sure about the efficiency of string. Replace in Microsoft VisualBasic runtime, especially after reading the code rewritten by C #, it is even more difficult.
Vbstring. the primary cause of the Replace () Efficiency problem is that the split () and join () operations will cause a large number of string fragments, which costs a lot for the managed system. In addition, the higher the replacement frequency in the string, the more obvious the loss of performance. We will see this problem in the following test examples. Therefore, the starting point of this replaceex () implementation is to avoid generating string fragments and the implementation code is not complex, as shown below:
Private Static string replaceex (string original, string pattern, string replacement)
{
Int count, position0, position1;
Count = position0 = position1 = 0;
String upperstring = original. toupper ();
String upperpattern = pattern. toupper ();
Int Inc = (original. Length/pattern. Length) * (replacement. Length-pattern.Length );
Char [] chars = new char [original. Length + math. Max (0, Inc)];
While (position1 = upperstring. indexof (upperpattern, position0 ))! =-1)
{
For (INT I = position0; I <position1; ++ I) chars [count ++] = original [I];
For (INT I = 0; I <replacement. length; ++ I) chars [count ++] = replacement [I];
Position0 = position1 + pattern. length;
}
If (position0 = 0) Return Original;
For (INT I = position0; I <original. length; ++ I) chars [count ++] = original [I];
Return new string (chars, 0, count );
}
There are only 18 lines of code. If it is complicated, won't everyone agree?
In order to save trouble, I used the test code of the inspiration brother. His test case is :"Replace "ABC" in the string "abc republic of china" with "people". Note: The Source substring is "ABC", and "ABC" is to be replaced ", the purpose is to test case insensitive. In order to test the efficiency, I would like to accumulate the test string for 1000 times, and then perform a loop test for 1000 times.".
The test data is as follows:
Regexp VB Vbreplace Replaceex Replace |
= 2.05845295980355 s = 0.684810220293361 s = 0.679955692692786 s = 0.535100131441287 s fastest! = 0.0564451627231953 S (string. Replace () for reference only) |
//. NET Framework 1.1, Windows XP SP2 en, P4 2.4g 512 M.
Because substring and substringb are too slow, I have no patience to wait for the results, so I canceled the test on them. As mentioned above, if the replacement frequency of the replaced string increases, the overhead of the Methods VB and vbreplace will also increase dramatically. Let's look at my test data, without changing the number of cycles of the test program, just modify: String segment = "abc republic of china"; one sentence.
1. String segment = "Republic of China ABC ";
Regexp VB Vbreplace Replaceex Replace |
= 3.75481827997692 s = 1.52745502570857 s = 1.46234256029747 s = 0.797071415501132 s !!! = 0.178327413120941 s |
// Replaceex> vbreplace> VB> Regexp
2. String segment = "ABC in China ABC and ABC in China ";
Regexp VB Vbreplace Replaceex Replace |
= 5.30117431126023 s = 2.46258449048692 s = 2.5018721653171 s = 1.00662179131705 s !!! = 0.233760994763301 s |
// Replaceex> VB> vbreplace> Regexp
3. String segment = "ABC in China and ABC in China ";
Regexp VB Vbreplace Replaceex Replace |
= 7.00987862982586 s = 3.61050301085753 s = 3.42324876485699 s = 1.14969947297771 s !!! = 0.277254511397398 s |
// Replaceex> vbreplace> VB> Regexp
4. String segment = "abcabcabcabcabcabcabcabcabc ";
Regexp VB Vbreplace Replaceex Replace |
= 13.5940090151123 s = 11.6806222578568 s = 11.1757614445411 s = 1.70264153684337 s !!! (Overwhelming advantage) = 0.42236820601501 s |
// Replaceex> vbreplace> VB> Regexp
Do you think replaceex is invincible and fast? In fact, I also hope that, but in extreme cases, replaceex won't work when there is no pattern to be replaced in the segment :(
5. String segment = "aabbcc ";
Regexp VB Vbreplace Replaceex Replace |
= 0.671307945562914 s = 0.32356849823092 s = 0.316965703741677 s !!! = 0.418256510254795 s = 0.0453026851178013 s |
// Vbreplace> VB> replaceex> Regexp
In the 5th tests, the reason for the low efficiency of replaceex is that all of its consumption is spent on these two statements:
String upperstring = original. toupper ();
String upperpattern = pattern. toupper ();
If we use compareinfo and compareoptions for the indexof operation, although case conversion can be avoided for strings, the efficiency is lower than the current implementation of replaceex in other cases except for the extreme cases in Test 5.
You are welcome to test the running efficiency and provide more optimization solutions :)
BTW: record test data without multiple averages, but it is best not to use the running results after the first compilation.