Wednesday, March 13, 2013

Performance analysis of Enum.Parse vs Enum.TryParse

For the scenerio I am testing, for the below two approaches I found that approach I is better performing than the Approach II.

     //Approach I
            for (int i = 0; i < 100000; i++)
            {
                EnumRepository eRepository1 = (EnumRepository)Enum.Parse(typeof(ECCRepository), "GHOUSE");
            }
           
            //Approach II
            for (int i = 0; i < 100000; i++)
            {
                EnumRepository eRepository;
                Enum.TryParse("GHOUSE", out eRepository);
            }

            stopwatch.Stop();
            long timespan1 = stopwatch.ElapsedTicks;
            TimeSpan timespan2 = stopwatch.Elapsed;

            Console.WriteLine("Time taken:" + timespan1);

No comments:

Post a Comment