Performance Comparison Between the replace method and Regular Expression

Source: Internet
Author: User

there is a small demand for today's project: Replace the carriage return symbol in the string with other symbols (such as "
"). For different situations, \ r \ n is used as the carriage return character in some systems, and \ n is used only as the carriage return character. Previously, the replace method of the string class was used for connection replacement multiple times. Today, I suddenly want to change it to a regular expression for one time, but I am afraid that the performance will be too high, so I wrote the following test Code :

Using system; using system. diagnostics; namespace consoleapplication1 {class program {static void main (string [] ARGs) {string a = "11111 \ n 22222 \ r 33333 \ n \ r 44444 \ r 55555 \ n 66666 \ r"; console. write (replace (A) + "\ n"); console. write (regreplace (a, "(\ r | \ n) +", "*") + "\ n"); stopwatch Sw = new stopwatch (); int COUNT = 50000; int times = 5; long result = 0; For (INT I = 0; I <times; I ++) {result + = test (SW, count, a, false);} console. write ("\ n {0} Times × {1} test, [Replace] method Average Speed per round: {2} \ n", Count, times, result/times); console. write ("\ n"); Result = 0; For (INT I = 0; I <times; I ++) {result + = test (SW, Count,, true);} console. write ("\ n {0} Times × {1} rounds of tests, [regular expression] method Average Speed per round: {2} \ n", Count, times, result/times); console. read ();} static string regreplace (string strsrc, string strregpattern, string strreplace) {return system. text. regularexpressions. regEx. replace (strsrc, strregpattern, strreplace);} static string Replace (string strsrc) {return strsrc. replace ("\ r \ n ","*"). replace ("\ n \ r ","*"). replace ("\ n ","*"). replace ("\ r", "*");} static long test (stopwatch SW, int count, string a, bool useregularexpressions) {int I = 0; Sw. reset (); Sw. start (); for (I = 0; I <count; I ++) {If (useregularexpressions) {regreplace (a, "(\ r | \ n) + ", "*") ;}else {Replace (a) ;}} SW. stop (); console. write (SW. elapsedmilliseconds + "\ n"); Return SW. elapsedmilliseconds ;}}}

Output result:

11111*22222*33333*44444*55555*66666
11111*22222*33333*44444*55555*66666

94
89
88
86
83

50000x5 tests, [Replace] method Average Speed per round: 88

333
327
321
327
332

50000x5 tests, [regular expression] method Average Speed per round: 328

It can be seen that regular expressions are more than twice slower, about 328/88 = 3.7 times slower (of course, there are some differences between changing the length of strings and the number and position of carriage returns)

Note:Edwin LiuIn the reply, the regular expression compilation and pushing speed is faster. Today, I changed the test code.

Using system; using system. diagnostics; using system. text. regularexpressions; namespace consoleapplication1 {class program {static void main (string [] ARGs) {RegEx Reg = new RegEx ("(\ r | \ n) +", regexoptions. compiled); string a = "11111 \ n 22222 \ r 33333 \ n \ r 44444 \ r 55555 \ r 66666"; console. write (replace (A) + "\ n"); console. write (Reg. replace (a, "*") + "\ n"); stopwatch Sw = new stopwatch (); int COUNT = 50000; int times = 5; long result = 0; for (INT I = 0; I <times; I ++) {result + = test (SW, Count, A, false);} console. write ("\ n {0} Times × {1} test, [Replace] method Average Speed per round: {2} \ n", Count, times, result/times); console. write ("\ n"); Result = 0; For (INT I = 0; I <times; I ++) {result + = test (SW, Count,, true);} console. write ("\ n {0} Times × {1} rounds of tests, [regular expression] method Average Speed per round: {2} \ n", Count, times, result/times); console. read ();} static string Replace (string strsrc) {return strsrc. replace ("\ r \ n ","*"). replace ("\ n \ r ","*"). replace ("\ n ","*"). replace ("\ r", "*");} static long test (stopwatch SW, int count, string a, bool useregularexpressions) {int I = 0; regEx Reg = new RegEx ("(\ r | \ n) +", regexoptions. compiled); Sw. reset (); Sw. start (); for (I = 0; I <count; I ++) {If (useregularexpressions) {Reg. replace (a, "*") ;}else {Replace (a) ;}} SW. stop (); console. write (SW. elapsedmilliseconds + "\ n"); Return SW. elapsedmilliseconds ;}}}

New test results:

11111*22222*33333*44444*55555*66666
11111*22222*33333*44444*55555*66666

100
93
86
86
84

50000x5 tests, [Replace] method Average Speed per round: 89

204
200
201
210
190

50000x5 tests, [regular expression] method Average Speed per round: 201

A rough comparison: After compilation and push, the speed is 201/89 = 2.3 times, which is indeed improved by 3.7 times. However, compared with the string type, the replace method can still be considered as slow. (However, in terms of convenience, it is quite convenient to use regular expressions for some complex functions)

Ii. Silverlight 4.0 Environment

Note: There is no stopwatch class in Silverlight, so datetime is used. now timing is replaced, but the result may not be accurate. In addition, the regular expression in Silverlight does not have the compile push function, so you can only use the original method.

Using system; using system. diagnostics; using system. windows; using system. windows. controls; using system. text. regularexpressions; namespace silverlightapplication1 {public partial class mainpage: usercontrol {public mainpage () {initializecomponent (); this. loaded + = new routedeventhandler (mainpage_loaded);} void mainpage_loaded (Object sender, routedeventargs E) {string a = "11111 \ n 22222 \ r 33333 \ n \ r 44444 \ r 55555 \ n 66666 \ r"; debug. writeline (replace (a); debug. writeline (regreplace (a, "(\ r | \ n) +", "*") + "\ n"); int COUNT = 50000; int times = 5; double result = 0; For (INT I = 0; I <times; I ++) {result + = test (count, A, false);} debug. writeline ("{0} X {1} rounds of tests, [Replace] method Average Speed per round: {2} \ n", Count, times, result/times ); result = 0; For (INT I = 0; I <times; I ++) {result + = test (count, A, true);} debug. writeline ("{0} X {1} rounds of tests, [regular expression] method Average Speed per round: {2} \ n", Count, times, result/times);} string regreplace (string strsrc, string strregpattern, string strreplace) {return system. text. regularexpressions. regEx. replace (strsrc, strregpattern, strreplace);} string Replace (string strsrc) {return strsrc. replace ("\ r \ n ","*"). replace ("\ n \ r ","*"). replace ("\ n ","*"). replace ("\ r", "*");} double test (INT count, string a, bool useregularexpressions) {int I = 0; datetime _ start = datetime. now; for (I = 0; I <count; I ++) {If (useregularexpressions) {regreplace (a, "(\ r | \ n) + ", "*") ;}else {Replace (a) ;}} datetime _ end = datetime. now; timespan span = _ end-_ start; debug. writeline (span. totalmilliseconds); Return span. totalmilliseconds ;}}}

Output result:

11111*22222*33333*44444*55555*66666
11111*22222*33333*44444*55555*66666

78.0002
93.6001
93.6002
78.0001
93.6002
50000x5 rounds of tests, the average speed of the [Replace] method per round: 87.36016

405.6007
405.6007
483.6009
405.6007
405.6007
50000x5 tests, [regular expression] method Average Speed per round: 421.20074

As you can see, it basically follows the ConsoleProgramAn order of magnitude (because the underlying CLR is basically similar, this is also expected, but it seems that the Silverlight regular expression is a little slower, it is estimated that there is a lot to do with the no compilation push function)

Iii. as3.0 Testing

Note: Some experts in the garden said that the performance of as3.0 was about 80% of that of Silverlight a few days ago, so I finally tested it in as3.0, but note that: because the string replace method in actionscript3.0 is the same as that in JS, only the string found for the first time can be replaced by default. Therefore, to replace the string completely, you can only use a regular expression.

Import flash. utils. timer; function Replace (strsrc: string): String {var mypattern: Regexp =/\ r | \ n/GI; return strsrc. replace (mypattern, "*");} function test (strsrc: String, Count: uint): int {var I: uint = 0; VaR _ start = gettimer (); for (I = 0; I <count; I ++) {Replace (strsrc);} VaR _ end = gettimer (); var elapsedtime = _ end-_ start; trace (elapsedtime); Return elapsedtime;} var A: String = "11111 \ n 22222 \ r 33333 \ n \ r 44444 \ r 55555 \ n 66666 \ r "; trace (replace (a); var count: uint = 50000; var times: uint = 5; var rlt: Number = 0; For (var I: uint = 0; I <times; I ++) {rlt + = test (A, count);} trace (count, "Times ×", times, "round test, [Replace] method Average Speed per round: ", rlt/times );

Output result:

11111*22222*33333 ** 44444 ** 55555*66666
1547
1508
1509
1515
1504
50000x5 rounds of tests, the average speed of the [Replace] method per round: 1516.6

However, this result is exaggerated.

Note: I tested several more items this morning. It seems that the result is faster. It is estimated that some programs were not closed during the test yesterday.

11111*22222*33333 ** 44444 ** 55555*66666
1048
1002
1001
1000
1009
50000x5 rounds of tests, the average speed of the [Replace] method per round: 1012

Of course, in the above example, the GI flag is added, that is, global search, and Case sensitivity is ignored. If the case sensitivity is removed, VAR mypattern: Regexp =/\ r | \ n/GI; vaR mypattern: Regexp =/\ r | \ n/g; faster

11111*22222*33333 ** 44444 ** 55555*66666
1015
971
972
970
971
50000x5 rounds of tests, the average speed of the [Replace] method per round: 979.8

 

Note: The test in this article is very extensive. It mainly refers to taking a rough look, so that you can have an impression in your mind. You are welcome to perform more accurate tests.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.