Wednesday, March 13, 2013

Checking the Performance of GetNumericValue vs ConvertInt32Default

            Stopwatch w = new Stopwatch();
            w.Start();
            for (int i = 0; i < 10000000; i++)
            {
                if (Char.GetNumericValue('1') > 0)
                {
                }
            }

            w.Stop();
            Console.WriteLine("GetNumericValue Approach = " + w.ElapsedTicks);
            w.Reset();
            w.Start();
            for (int i = 0; i < 10000000; i++)
            {
                if (ConvertInt32Default('1') > 0)
                {

                }
            }
            w.Stop();
            Console.WriteLine("ConvertInt32Default Approach = " + w.ElapsedTicks);
           
            Console.ReadLine();


public static System.Int32 ConvertInt32Default(char input)
        {
            System.Int32 value;

            bool status = int.TryParse(input.ToString(), out value);
            if (status == true)
                return value;
            else
                return default(System.Int32);
        }


Output:
GetNumericValue Approach = 337396
ConvertInt32Default Approach = 3653561

No comments:

Post a Comment