AvocadoSoftware.com

Software For Hardcore Developers
Welcome to AvocadoSoftware.com Sign in | Join | Help
in Search

Derick Baileys old blog archives - go to derickbailey.com for new contents

In Response to: Regex vs. string.IndexOf

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:

  1. 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.
  2. 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

Published Monday, November 05, 2007 3:24 PM by dredge
Filed Under: ,
New Comments to this post are disabled

This Blog

Post Calendar

<November 2007>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

Advertisement

News

this is my old blog archives - go to http://derickbailey.com for updates

Syndication

Advertisement

Powered by Community Server, by Telligent Systems