Fields vs. properties performance on the Xbox 360.

I took Sam Allen’s performance test and ran a similar test (below) on the Xbox 360 with XNA.

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

public FieldPropertyTest(Game game)
    : base(game)
{

    long[] results1 = new long[10];
    long[] results2 = new long[10];

    Property = "string";
    Field = "string";
    const int m = 1000000;
    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();
        results1[x] = s1.ElapsedMilliseconds;
        results2[x] = s2.ElapsedMilliseconds;
    }
}

The results:

Get:
Property: 341 ms
Field: 307 ms

Set:
Property: 85 ms
Field: 66 ms

This performance difference from the more robust .NET CLR used in Allen’s tests is owing to the fact that the .NET Compact CLR (which the Xbox 360 uses) doesn’t inline functions at all — to my knowledge.

Of course, the small size of the numbers above is good news — it took 10 million gets on a field to produce a 34ms difference.
It’s hard to imagine a scenario where using a field in place of a property would be a relatively large enough performance gain to affect your framerate.

There are also the usual caveats with micro-benchmarks like these, and the difference may be smaller (or larger) than what you see above, in real-world uses.

So, I’d take Jeff Atwood’s advice and simply always use (auto-)properties, unless you have a very tight performance sensitive loop that is accessing many properties.

Edit:
The NetCF team blog states that getters/setters are inlined, which is probably a source of some confusion. It’s also possible that my results above are wrong, if I’ve fallen into a micro benchmarking trap. Let me know if you find some results opposing what I’ve written above.

This entry was posted in Coding, XNA and tagged , , . Bookmark the permalink.

5 Responses to Fields vs. properties performance on the Xbox 360.

  1. denis says:

    thanks for sharing sm info abt xbox. nice to learn new things! great post!

  2. 3nixios says:

    I Quoth: “Property: 85 ms, Field: 66 ms”
    Indeed this is a meager gain, but I think it would be interesting to count the number of property gets/sets in a big project, the gain may actually be worthwhile.
    We also don’t know how things work with caching, what sort of memory usage problems properties entail.
    And finally, I think it’s just “good practice” to not use the Setter of a property when it is not needed.

  3. olhovsky says:

    3nixios: Actually I’ve been meaning to write an update to this post. My test uses reference types. Value types would copy across the whole type (e.g. a 4×4 float matrix would copy 16 times as much data as copying a 4 byte reference as in my test above).

    So actually the performance gain from avoiding properties can be quite significant on the xbox, when value types are involved.

  4. Elvie Knoeppel says:

    The Xbox 360 console and PS3 are both gaming consoles which have taken the gaming arena by storm. While Xbox 360 console may be the successor of Xbox and is manufactured by Microsoft, PS3, also called Ps3 may be the successor of PS2 and is manufactured by Sony. Both Xbox 360 console and PS3 are masters within their own areas, however there are specific features in Xbozx360 that means it is conquer Sony’s PS3. In this specific article we now have come up with a few of the features which make the Xbo360 a far greater gaming system than PS3.’

    Most current write-up provided by our own webpage
    http://www.caramoan.ph/caramoan-resorts/

  5. Whoa… such a valuable website

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>