News

Internet - .Net user group technical events, emerging .Net technologies;
General - Eco-travel, health and fitness, current events;
Community - Active volunteer with Hands On Miami, Non-Profit Ways;
.Net Framework - Detected 3.5 SP1 .NET Framework. No update needed ;
creative zen converter

Tweets













Static fields and instance fields in the C#

Static fields do not depend on an instance expression and therefore require one fewer level of indirection on each load and store; instance fields therefore result in more instructions being executed and slightly worse performance. Here we compare instance fields in a class to static fields in a class declaration.

Static fields are simpler because they do not require that extra level of indirection, and this is reflected in better performance.

using System;
using System.Diagnostics;

class Test1
{
    int _a; // Instance fields:
    int _b;
    int _c;
    public void X()
    {
        this._a++; // Change instance field values:
        this._b++;
        this._c++;
    }
}

class Test2
{
    static int _a; // Static fields:
    static int _b;
    static int _c;
    public void X()
    {
        _a++; // Change static field values:
        _b++;
        _c++;
    }
}

class Program
{
    const int _max = 200000000;
    static void Main()
    {
        Test1 test1 = new Test1(); // Instantiate instance fields
        Test2 test2 = new Test2(); // Instantiate

        var s1 = Stopwatch.StartNew();
        for (int i = 0; i < _max; i++)
        {
            test1.X(); // Instance fields:
            test1.X();
            test1.X();
            test1.X();
            test1.X();
        }
        s1.Stop();
        var s2 = Stopwatch.StartNew();
        for (int i = 0; i < _max; i++)
        {
            test2.X(); // Static fields:
            test2.X();
            test2.X();
            test2.X();
            test2.X();
        }
        s2.Stop();
        Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) / _max).ToString("0.00") + " ns");
        Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) / _max).ToString("0.00") + " ns");
        Console.Read();
    }
}


Out put:
10.51 ns  (Time for instance field usage)
10.21 ns  (Time for static field usage)

Introduces three classes into the compilation unit. The Test1 and Test2 classes are similar but the Test1 class has instance fields while the Test2 class has static fields. If you create many Test1 class instances, each will have its own copy of the fields; if you create many Test2 instances, there will only be one copy of the fields in it. The instance method on the classes mutates the value of the fields. The Program class simply runs a benchmark of the two X methods.

When executed, this program will change the value of the instance fields in Test1 through the method X. It will then change the value of the static fields located inside the Test2 declaration through the other method X. Typically, the Test2.X method is slightly faster on the author's computer, by about 0.3 nanoseconds per loop iteration. This shows that evaluating the instance expression for the instance fields in Test1 causes a slowdown over the static fields in Test2.

Static fields and instance fields are evaluated in the Common Language Runtime (CLR).   an instance field is one that can be created many times in the environment, while a static field exists only one place.   An instance field expression must first evaluated.  In the Intermediate Language (IL), the inctance expressions is equal to the first argument to an instance method.   The first argurment to an instance method is always the instance itself.

 When accessing an instance field, the instance expression is loaded onto the evaluation stack from the parameters with "ldarg.0". This step is not required for a static field because the static field does not require that extra level of indirection. Therefore, using instance fields will insert an additional instruction into the IL for accesses, both reads and writes. This accounts for the slowdown when using instance fields.

Static and instance field performance is not the primary one to make in non-trivial programs. If you need instances of a type, then please use instance fields as they will make your program correct. However, the point here was to examine the evaluation process for instance fields and compare it to that of static fields, and also to test the actual time difference. In some cases, you can change instance fields to static fields for performance improvements.

Saturday, February 06, 2010 10:02 AM

Feedback

No comments posted yet.


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: