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...
    }
Follow

Get every new post delivered to your Inbox.

Join 70 other followers