C# Property Test

You are interested in seeing whether the JIT inlines properties in the C# language and .NET Framework and how this affects performance in comparison to fields. Properties have getters and setters and generate different MSIL. Here we examine the performance of the JIT compilation on properties, using the C# programming language.

~~~ Property test benchmark in C# ~~~                       
    Based on .NET 3.5 SP1.                                  
    Properties were inlined and had equivalent performance. 

Property get/set:   604.6 ms 
Field read/assign:  603.6 ms 

Measuring properties

Here we see the C# program tested, which benchmarks with Stopwatch and performs the two loops ten times. Each inner loop has 100,000,000 iterations. The property's backing store is a string, as is the field. The performance impact of the static keyword is probably not important here. You can see the benchmark results above.

--- Program that benchmarks properties (C#) ---

using System;
using System.Diagnostics;

class Program
{
    static string _backing; // Backing store for property
    static string Property  // Getter and setter
    {
        get
        {
            return _backing;
        }
        set
        {
            _backing = value;
        }
    }
    static string Field;    // Static field

    static void Main()
    {
        const int m = 100000000;
        for (int x = 0; x < 10; x++) // Ten tests
        {
            Stopwatch s1 = new Stopwatch();
            s1.Start();
            for (int i = 0; i < m; i++) // Test property
            {
                Property = "string";
                if (Property == "cat")
                {
                }
            }
            s1.Stop();
            Stopwatch s2 = new Stopwatch();
            s2.Start();
            for (int i = 0; i < m; i++) // Test field
            {
                Field = "string";
                if (Field == "cat")
                {
                }
            }
            s2.Stop();
            Console.WriteLine("{0},{1}",
                s1.ElapsedMilliseconds,
                s2.ElapsedMilliseconds);
        }
        Console.Read();
    }
}

Results. There was no difference in performance with the property shown and the direct field. I conclude that the property access is inlined completely.

Property performance overview

To find out the answers here, I researched the problem on Google and then constructed a test harness. On Microsoft's Abhinaba Basu's blog, it is stated that: "Since get/set functions will be inlined, I don't think there'd be any perf hit in using them. However, I didn't profile to see the diff."

Visit blogs.msdn.com.

More mentions. On the forums I found some questions and responses urging people to profile, but not actual results posted, so my next step was to benchmark. This article contains the results of that benchmark.

Summary

Here we proved that the JIT compiler is smart enough to inline properties that don't have logic inside of them, so that they are as efficient as fields. However, there is still MSIL generated for properties, which means the JIT must deal with them, not just the C# language compiler.

See String Property Use.

See Class Overview.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen