Can a static constructor reduce the performance of accessing static methods?

The static constructor is executed the first time a static member is accessed. Knowing this, I have a few questions:

  • Does this mean that every time I access the static method, the runtime should check if the static constructor was called?
  • Does this cause a performance failure?
  • Use static constructorless classes avoiding this performance?

[EDIT] I would like to clarify that I am NOT involved in micro-optimization.
I ask this question because it is a design decision. If a static constructor strikes a performance hit, then I will design my code with this in mind and be more aware of decisions that may affect performance.

Here is an example to illustrate my question. Will there be any benefit in adopting the Independent method and putting it in a separate static class? Thus, it would not be necessary to check whether the static Test was initialized. [ Update See my answer below for a simpler, simpler example].

 static class Test { // Static constructor with dependent method: static int x; static Test() { x = 5; } static int Dependent() { return x; } // Static, independent method: static int Independent(int y) { return y+1; } } 

Here is a quote from the C # spec on static constructor:

The execution of the static constructor is triggered first of the following events to occur in the application domain:

  • An instance of the class is created.
  • A reference to any of the static members of the class.
+6
source share
3 answers

Due to the lack of answers and in the direction of @Jobo, I decided to check it out for myself.

Here are my test classes:

 static class WithConstructor { static WithConstructor(){ } public static int Square(int x){ return x*x; } } static class NoConstructor { public static int Square(int x){ return x*x; } } 

Compiled for release using .NET 4.0, the results were very consistent:

  ╔════════════════════════════════╦══════════════ ═════════╗
 ║ Iterations: ║ With Constructor ║ 4513 ms ║ Improvement: ║
 ║ 1 000 000 000 ║ No Constructor ║ 3072 ms ║ 33% ║
 ╚════════════════════════════════╩══════════════ ═════════╝

Therefore, I am going to answer my questions:

  • If there is a static constructor, then the static method will be subject to a (microscopic) result, because the beforefieldinit flag beforefieldinit always be checked.

  • If a static constructor does not exist, then the method will not be affected by performance.

+7
source

Why not check it out yourself?

Call your Independent method several times as above. Then create your own static class with the same method and name it as many times.

Use http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx to measure.

My guess would be unimportant ...

You can also write something in the console in your static constructor to check if it was called. Clarification for yourself will last longer than some kind of theoretical answer, only 2 cents.

+2
source
Static constructor

may reduce the performance of the first calling method. Indead, the first caller is replaced with a check if the static constructor is already called, but the other caller if not affected.

0
source

Source: https://habr.com/ru/post/901486/


All Articles