Loading ...

Difference between implicit and explicit static constructors | CodeAsp.Net

Difference between implicit and explicit static constructors

  • 0
    /5
    Avg: 0/5: (0votes)
  • (1)
  • (6304)
Published: 10/25/2009 by Raghav Khunger

In this article I will discuss the performance  regarding difference between implicit and explicit static constructors .There are two ways by which we can declare static fields

Implicit:

 

 public class TestImplicitConstructor
    {
        public static int I;
        public static double D;
        public static string MyString;
    }



Explicit:

 

 public class TestExplicitConstructor
    {
        public static int I=1;
        public static double D=10f;
        public static string MyString = "Test String";

        static TestExplicitConstructor()
        {
            I = 1;
            D = 10f;
            MyString = "Test String";
        }
    } 



We can initialize our static fields in both of the ways, but there is time difference between initializing the static fields between them.


There is good explanation by K. Scott Allen at


http://msdn.microsoft.com/hi-in/magazine/cc163857(en-us).aspx#S1


He states:


Notice that ImplicitConstructor has an additional metadata flag named beforefieldinit. This flag allows the runtime to execute the type constructor method at any time it chooses, as long as the method executes before the first access to a static field for the type. In other words, beforefieldinit gives the runtime a license to perform aggressive optimizations. Without beforefieldinit, the runtime must run the type constructor at a precise time—just before the first access to static or instance fields and methods of the type. When an explicit type constructor is present, the compilers will not mark the type with beforefieldinit, and the precise timing restrictions lead to the performance drop hinted at by FxCop. This rule holds in both C# and Visual Basic.

So I made a small test over it and wrote a code in windows project.

 

 

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace Implicit_and_Explicit_Static_Constructors
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnImplicitTest_Click(object sender, EventArgs e)
        {
            var iterationCount = Convert.ToDouble(cmNoOfIterations.Text);
            var watch = new Stopwatch();
            watch.Start();
            for (var i = 0; i < iterationCount; i++)
            {
                TestImplicitConstructor.I = i;
                TestImplicitConstructor.D = 10f;
                TestImplicitConstructor.MyString ="Test String";
            }
            watch.Stop();

            txtOutput.Text = string.Format("TestImplicitConstructor: {0} ms \r\n", watch.ElapsedMilliseconds) + txtOutput.Text;
        }

        private void btnExplicitTest_Click(object sender, EventArgs e)
        {
            var iterationCount = Convert.ToDouble(cmNoOfIterations.Text);
            var watch = new Stopwatch();
            watch.Start();
            for (var i = 0; i < iterationCount; i++)
            {
                TestExplicitConstructor.I = i;
                TestExplicitConstructor.D = 10f;
                TestExplicitConstructor.MyString = "Test String";
            }
            watch.Stop();

            txtOutput.Text = string.Format("TestExplixitConstructor: {0} ms \r\n", watch.ElapsedMilliseconds) + txtOutput.Text;
        }
    }
    public class TestImplicitConstructor
    {
        public static int I;
        public static double D;
        public static string MyString;
    }
    public class TestExplicitConstructor
    {
        public static int I=1;
        public static double D=10f;
        public static string MyString = "Test String";

        static TestExplicitConstructor()
        {
            I = 1;
            D = 10f;
            MyString = "Test String";
        }
    }
}



 

Here is the screen shot I got by running test over 10000000 iterations

 

 

Now I tested for 1000000000 iterations :

 

 

As you saw from above test Implicit type constructors are much faster than explicit type. Here is the attached Implicit and Explicit Static Constructor  test project  rar file.



Do let me know your feedback, comments.

 

Comments (1)

  
Jeff
Jeff said:
Your test is invalid.The implicit case should be code to have direct assignment on the Field.ex. public static int I=1;The explicit case should not assign directly to the field and should assign within a static constructor.This way it shows that you can initialize values in two different ways that lead to very different performance.  
3/25/2011
 · 
 
by
  • :*
  • :*
  • :
 *

Top articles

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?