D'OH! I noticed a huge bug in my original code. here is the correct version and correct numbers. Looks like for this simple test, an IndexOf is faster.
..
Just read Ayende's post on Regex vs. string.IndexOf. I have two complaints about the test code that he provided:
- He's getting the start time for the regex before he is compiling the regex. in a real app, you should compile the regex once (during a static constructor or at app initialize) to ensure the regex compiling does not cause a performance hit when you need to run the regex.
- The regex is not optimized. he is basically using the regex to mimic the string.IndexOf functionality instead of property writing the regex to perform better.
Here is my updated version of his code:
static void Main()
{
RunTest();
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
private static Regex r = new Regex("(?<name>.*?),", RegexOptions.Compiled);
private static void RunTest()
{
string testStr = "select foo, bar, x, y, z, 5 from Items";
int count = 500000;
DateTime start = DateTime.Now;
for (int i = 0; i < count; i++)
{
int last = 0, current = 0;
while ((current = testStr.IndexOf(',', current)) != -1)
{
string x = testStr.Substring(last, current - last);
current = last = current + 1;
}
}
Console.WriteLine("IndexOf: " + (DateTime.Now - start));
start = DateTime.Now;
for (int i = 0; i < count; i++)
{
MatchCollection matches = r.Matches(testStr);
foreach (Match match in matches)
{
string y = match.Groups["name"].Value;
}
}
Console.WriteLine("Regex: " + (DateTime.Now - start));
}
Here is my results. I have run it dozens of times, and this appears to be the average:
IndexOf: 00:00:00.2657899
Regex: 00:00:07.4912247