65.9K
CodeProject is changing. Read more.
Home

Counting lines in a string

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Feb 26, 2012

CPOL
viewsIcon

19711

Great analysis!I found out that Regex can be accelerated by a factor of about two.Instead of new Regex(@"\n", RegexOptions.Compiled|RegexOptions.Multiline);you can speed up by using:new Regex(@"^.*?$", RegexOptions.Compiled|RegexOptions.Multiline);But admittedly, nothing beats...

Great analysis! I found out that Regex can be accelerated by a factor of about two. Instead of
new Regex(@"\n", RegexOptions.Compiled|RegexOptions.Multiline);
you can speed up by using:
new Regex(@"^.*?$", RegexOptions.Compiled|RegexOptions.Multiline);
But admittedly, nothing beats the native methods (IndexOf). [EDIT] My statement above is wrong: I did compare "$" (and not "\n") against "^.*?". The measurments show that "\n" is the fastest of all Regex matches, while "$" is the slowest (5 times slower than "\n"...!). That's a real surprise to me. The comparison:
Regex Match[ms] for 2.500.000 linesRegexOptions
\n1847Compiled|Singleline
\n1851Compiled|Multiline
^.*$2282Compiled|Multiline
^.*?$5327Compiled|Multiline
$10100Compiled|Multiline
As a comparison: IndexOf('\n') only takes 237 [ms]. [/EDIT]