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 lines | RegexOptions |
---|
\n | 1847 | Compiled|Singleline |
\n | 1851 | Compiled|Multiline |
^.*$ | 2282 | Compiled|Multiline |
^.*?$ | 5327 | Compiled|Multiline |
$ | 10100 | Compiled|Multiline |
As a comparison: IndexOf('\n') only takes 237 [ms].
[/EDIT]