Parsing vs Casting


I was working with non-databound DataGridView today and had to convert each of the data from the cells, which are stored as objects, into their correct types. My first naive inclination was to use the DateTime.Parse or the int.Parse methods but got to thinking and wondered if casting the object to the correct type would be more efficient. After researching the differences it turns out that casting it much more efficient (which make sense). But, just to convince myself, I ran a few tests to compare the time it takes to cast and to parse. The results and code are below

      static void Main(string[] args)
        {
            DateTimeTest();
            IntTest();
            Console.WriteLine();
            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
        }

        static void DateTimeTest()
        {
            long castingTime = 0;
            long parsingTime = 0;

            Stopwatch sw = Stopwatch.StartNew();
            object oDate = new DateTime();

            for (int i = 0; i < 500000; i++)
            {
                DateTime date = (DateTime)oDate;
                oDate = date.AddDays(1);
            }

            sw.Stop();
            castingTime = sw.ElapsedMilliseconds;

            sw.Reset();
            sw.Start();

            for (int i = 0; i < 500000; i++)
            {
                DateTime date = DateTime.Parse(oDate.ToString());
                oDate = date.AddDays(1);
            }

            sw.Stop();
            parsingTime = sw.ElapsedMilliseconds;

            Console.WriteLine("DateTime Test");
            Console.WriteLine("-------------");
            Console.WriteLine("Casting: " + castingTime + "ms");
            Console.WriteLine("Parsing: " + parsingTime + "ms");

        }

        static void IntTest()
        {

            long castingTime = 0;
            long parsingTime = 0;

            Stopwatch sw = Stopwatch.StartNew();
            object oInt = 1;

            for (int i = 0; i < 500000; i++)
            {
                int integer = (int)oInt;
                oInt = integer++;
            }

            sw.Stop();
            castingTime = sw.ElapsedMilliseconds;

            sw.Reset();
            sw.Start();

            for (int i = 0; i < 500000; i++)
            {
                int integer = int.Parse(oInt.ToString());
                oInt = integer++;
            }

            sw.Stop();
            parsingTime = sw.ElapsedMilliseconds;

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Int Test");
            Console.WriteLine("-------------");
            Console.WriteLine("Casting: " + castingTime + "ms");
            Console.WriteLine("Parsing: " + parsingTime + "ms");
        }

        // Output:
        //
        // DateTime Test
        // -------------
        // Casting: 20ms
        // Parsing: 1689ms

        // Int Test
        // -------------
        // Casting: 8ms
        // Parsing: 121ms

        // Press enter to close...
    }

2 Responses to “Parsing vs Casting”

  1. Skyler Onken Says:

    Wow. thats fantastic! quite a difference!

    • Nick Olsen Says:

      True statement. But then again… I guess the only time it would really matter is when you are doing 500,000 casts or parses! Probably not much difference when performing one or two at a time.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: