Posted by: Cirilo Meggiolaro | 02/22/2009

Tip of the day #131 – Static constructors

Static constructors are used basically to initialize static objects data but may be used to perform tasks that need to run just once during the application life cycle.

Some facts first

  • It must be used to initialize static object data;
  • Static constructors don’t accept parameters;
  • Static constructors don’t have access modifiers;
  • You don’t have control over when the constructor will be executed;
  • Based on the fact that the static constructor runs just once, if an error occurs the static constructor will not be invoked again and any data that haven’t been initialized will remain uninitiliazed.

How to…

The syntax is pretty straightforward. You just need to add the static keyword before the class name:

static WorkflowHelper()
{
    /// Initializes static object data here.
}

Performance

I’ve heard / read some developers talking over performance penalties when using static constructors. I made three different benchmarkings to compare similar classes with both static and nonstatic constructors. The results are listed below:

Benchmarking 1 – Two classes implement a Singleton design pattern and return an instance of a WorkflowRuntime object (WWF). The first one initializes the WorkflowRuntime object when declared and the second one initializes the object from the static constructor.

Results for 1.000.000.000 iterations:

  • First execution: The class with static constructor took 121553 milliseconds while the class without the static constructor took 121561 milliseconds;
  • Second execution: The class with static constructor took 120929 milliseconds while the class without the static constructor took 120978 milliseconds;
  • Third execution: The class with static constructor took 126200 milliseconds while the class without the static constructor took 126367 milliseconds.

Benchmarking 2 – Two classes have a static integer member. The first class initializes the integer variable when declaring it while the second one uses a static constructor.

static class ClassWithStaticConstructor
{
    public static readonly int _testVariable;

    static ClassWithStaticConstructor()
    {
        _testVariable = 1;
    }
}

static class ClassWithoutStaticConstructor
{
    public static readonly int _testVariable = 1;
}

Results for 1.000.000.000 iterations:

  • First execution: The class with static constructor took 7637 milliseconds while the class without the static constructor took 4677 milliseconds;
  • Second execution: The class with static constructor took 7688 milliseconds while the class without the static constructor took 4671 milliseconds;
  • Third execution: The class with static constructor took 7680 milliseconds while the class without the static constructor took 4683 milliseconds.

Benchmarking 3 – Two classes have a static string member. The first class initializes the string variable when declaring it while the second one uses a static constructor.

static class ClassWithStaticConstructor
{
    public static readonly string _testVariable;

    static ClassWithStaticConstructor()
    {
        _testVariable = “Hello benchmarking”;
    }
}

static class ClassWithoutStaticConstructor
{
    public static readonly string _testVariable = “Hello benchmarking”;
}

Results for 1.000.000.000 iterations:

  • First execution: The class with static constructor took 4670 milliseconds while the class without the static constructor took 7685 milliseconds;
  • Second execution: The class with static constructor took 4672 milliseconds while the class without the static constructor took 7659 milliseconds;
  • Third execution: The class with static constructor took 4641 milliseconds while the class without the static constructor took 7612 milliseconds.

Conclusion

You may have noticed that the benchmarking using integer variable showed different results than the benchmarking with reference type objects. When you declare a numeric type without initialize it the value zero is assigned as default to it. When you assign a value from inside the static constructor you are performing an additional task that will not happen when declaring and initializing on one shot.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: