C# performance analysis- how to count CPU cycles?

Posted by Lirik on Stack Overflow See other posts from Stack Overflow or by Lirik
Published on 2010-03-17T23:22:36Z Indexed on 2010/03/17 23:31 UTC
Read the original article Hit count: 852

Filed under:
|
|

Is this a valid way to do performance analysis? I want to get nanosecond accuracy and determine the performance of typecasting:

class PerformanceTest
{
    static double last = 0.0;
    static List<object> numericGenericData = new List<object>();
    static List<double> numericTypedData = new List<double>();

    static void Main(string[] args)
    {
        double totalWithCasting = 0.0;
        double totalWithoutCasting = 0.0;
        for (double d = 0.0; d < 1000000.0; ++d)
        {
            numericGenericData.Add(d);
            numericTypedData.Add(d);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int i = 0; i < 10; ++i)
        {

            stopwatch.Start();
            testWithTypecasting();
            stopwatch.Stop();
            totalWithCasting += stopwatch.ElapsedTicks;

            stopwatch.Start();
            testWithoutTypeCasting();
            stopwatch.Stop();
            totalWithoutCasting += stopwatch.ElapsedTicks;
        }

        Console.WriteLine("Avg with typecasting = {0}", (totalWithCasting/10));
        Console.WriteLine("Avg without typecasting = {0}", (totalWithoutCasting/10));
        Console.ReadKey();
    }

    static void testWithTypecasting()
    {
        foreach (object o in numericGenericData)
        {
            last = ((double)o*(double)o)/200;
        }
    }

    static void testWithoutTypeCasting()
    {
        foreach (double d in numericTypedData)
        {
            last = (d * d)/200;
        }
    }
}

The output is:

Avg with typecasting = 468872.3
Avg without typecasting = 501157.9

I'm a little suspicious... it looks like there is nearly no impact on the performance. Is casting really that cheap?

© Stack Overflow or respective owner

Related posts about c#

Related posts about Performance