ArticleDirectory
- String data for testing
- Test related methods
- Specific test code
In the previous article, I shared the custom implementation of an efficient string split method. Next I will share the string-related operation function replace. by decompiling the string replace method, the built-in implementation cannot view the specific implementation source code, so it cannot be inferred how the string replace method is implemented. However, out of curiosity, you can manually implement a corresponding replace function, from the test results, the efficiency is relatively better than the string replace method.
Test Description
To make the test more comprehensive, different content replacement and processing times are added to the test.
Cache-control: public, Max-age = 0
Content-encoding: Gzip
Content-Length: 9480
Content-Type: text/html; charset = UTF-8
Date: Wed, 31 Oct 2012 14:17:06 GMT
Expires: Wed, 31 Oct 2012 14:17:05 GMT
Last-modified: Wed, 31 Oct 2012 14:17:05 GMT
P3p: Cp = non DSP cor ADM cur Dev Tai our ind nav pre sta
P3p: Cp = non DSP cor ADM cur Dev Tai our ind nav pre sta
Server: Microsoft-Microsoft IIS/7.5
Set-COOKIE: Smark = branch = default & isproject = 1; domain = .codeplex.com; expires = Fri, 31-oct-2042 14:17:06 GMT; Path =/
Vary: Accept-Encoding
X-ASPnet-version: 4.0.30319
X-aspnetmvc-version: 4.0
X-powered-by: ASP. NET
Static void stringreplace (string value, string olddata, string newdata) {for (INT I = 0; I <count; I ++) {value. replace (olddata, newdata) ;}} static void stringextendreplace (string value, string olddata, string newdata) {for (INT I = 0; I <count; I ++) {stringextend. replace (value, olddata, newdata );}}
Static void main (string [] ARGs) {value. replace ("1", "010"); stringextend. replace (value, "1", "010"); console. setout (new system. io. streamwriter ("test.txt"); Count = 1; testreplace (value, "1", "010"); Count = 10; testreplace (value, "C ", "010"); Count = 100; testreplace (value, "W", "010"); Count = 1000; testreplace (value, "A", "010 "); count = 10000; testreplace (value, "0", "010"); Count = 100000; testreplace (value, ":", "010"); Count = 1; testreplace (value, "ASP. net "," aspx "); Count = 10; testreplace (value," 2012 "," 2048 "); Count = 100; testreplace (value," wed ", "MVC"); Count = 1000; testreplace (value, "content", "testopq"); Count = 10000; testreplace (value, "ASP. net "," aspx "); Count = 100000; testreplace (value," our "," BBQ "); console. out. flush (); console. read ();} static void testreplace (string value, string olddata, string newdata) {console. writeline ("========== [count: {0} \ t ({1} to {2})] ================ ", Count, olddata, newdata); system. diagnostics. stopwatch Sw = new system. diagnostics. stopwatch (); Sw. reset (); Sw. start (); stringextendreplace (value, olddata, newdata); Sw. stop (); console. writeline ("stringextendreplace: \ t {0} ms", SW. elapsed. totalmilliseconds); Sw. reset (); Sw. start (); stringreplace (value, olddata, newdata); Sw. stop (); console. writeline ("stringreplace: \ t {0} ms", SW. elapsed. totalmilliseconds); console. writeline ("");}
========= [Count: 1 (1 to 010)] ======================
Stringextendreplace: 0.1728 Ms
Stringreplace: 0.1654 Ms
========= [Count: 10 (C to 010)] ======================
Stringextendreplace: 0.0538 Ms
Stringreplace: 0.1002 Ms
========= [Count: 100 (W to 010)] ====================
Stringextendreplace: 0.4561 Ms
Stringreplace: 0.9015 Ms
========= [Count: 1000 (A to 010)] ====================
Stringextendreplace: 5.4919 Ms
Stringreplace: 11.0184 Ms
========= [Count: 10000 (0 to 010)] ====================
Stringextendreplace: 52.7016 Ms
Stringreplace: 96.3369 Ms
========= [Count: 100000 (: to 010)] ======================
Stringextendreplace: 633.8311 Ms
Stringreplace: 1025.9258 Ms
========= [Count: 1 (ASP. NET to aspx)] ================
Stringextendreplace: 0.0067 Ms
Stringreplace: 0.0084 Ms
========= [Count: 10 (2012 to 2048)] ======================
Stringextendreplace: 0.0424 Ms
Stringreplace: 0.0889 Ms
========= [Count: 100 (Wed to MVC)] ====================
Stringextendreplace: 0.4074 Ms
Stringreplace: 0.8477 Ms
========= [Count: 1000 (content to testopq)] ==================
Stringextendreplace: 4.5297 Ms
Stringreplace: 7.6571 Ms
========= [Count: 10000 (ASP. NET to aspx)] ================
Stringextendreplace: 43.2199 Ms
Stringreplace: 82.9951 Ms
========= [Count: 100000 (Our to BBQ)] ==================
Stringextendreplace: 442.286 Ms
Stringreplace: 841.7506 Ms
Method Source code
[Threadstatic] Static char [] mtempchars; protected static char [] gettempdata () {If (mtempchars = NULL) mtempchars = new char [1024*64]; return mtempchars ;} public static string Replace (string value, string olddata, string newdata) {char [] tmpchars = gettempdata (); int newpostion = 0; int oldpostion = 0; int length = value. length; int oldlength = olddata. length; int newlength = newdata. length; Int Index = 0; int copylength = 0; bool eq = false; while (index <value. length) {eq = true; For (int K = 0; k <oldlength; k ++) {If (value [index + k]! = Olddata [k]) {eq = false; break ;}} if (EQ) {copylength = index-oldpostion; value. copyto (oldpostion, tmpchars, newpostion, copylength); newpostion + = copylength; index + = oldlength; oldpostion = index; newdata. copyto (0, tmpchars, newpostion, newlength); newpostion + = newlength;} else {index ++ ;}} if (oldpostion <length) {copylength = index-oldpostion; value. copyto (oldpostion, tmpchars, newpostion, copylength); newpostion + = copylength;} return new string (tmpchars, 0, newpostion );}
Summary
[Threadstatic] is used to allocate a group of char [] to each thread to minimize the overhead of char. careful friends should find that the char [] size analyzed by each thread is 64 K. In other words, if this method is replaced with 64 K, it will be abnormal, but you can also set a maximum value for analysis. if necessary, add case-insensitive replacement for the function