Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Counting lines in a string

0.00/5 (No votes)
28 Feb 2012CPOL 19.7K  
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:
C#
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]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)