In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the static constructor of CLR Via C # call". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the static constructor called by CLR Via C #"?
CLR Via C# Summary:
1 static constructors are private, and access modifiers cannot be modified artificially.
The static constructor should not call the static constructor of the base class because static fields are not inherited to subclasses.
3 static constructors have one and only one in a type, and are unparameterized.
4 static fields can only be initialized in static constructors.
From the above four points, we can know that the static constructor is private, so the display area can not be called, about when JIT will generate code to call the static constructor. There are two theories. It is often called Precise and BeforeFieldInit. Precise mode when the JIT compiler generates the call: before creating the code of the type, and before accessing the non-inherited field or member code of the class. BeforeFieldInit mode when the JIT compiler generates the call: before the access fee inherits the static field code.
The main difference between the two methods is whether the time to call the static constructor is determined. Precise mode CLR will call the static constructor at a certain time, while BeforeFieldInit mode CLR can freely choose the time to call the static constructor. Taking advantage of this, CLR can choose the number of calls to the static constructor according to whether the type is loaded in the program domain, so that it can generate and execute faster code.
Let's look at a class that shows these two ways.
Public class UserPrecise {public static string _ name = "inline assignment: oec2003"; static UserPrecise () {_ name = "constructor assignment: oec2003";}} public class UserBeforeFieldInit {public static string _ name = "inline assignment: oec2003";}
The IL code shows that there is a BeforeFieldInit tag on the metadata of UserBeforeFieldInit, as shown in the following figure:
Since it is mentioned above that CLR can choose the number of times to call the constructor to generate code that executes faster, let's write a piece of test code to see what happens.
Public sealed class Program {static void Main (string [] args) {const Int32 iterations = 1000 * 1000 * 1000; Test1 (iterations); Test2 (iterations);} private static void Test1 (Int32 iterations) {Stopwatch sw = Stopwatch.StartNew (); for (Int32 I = 0; I
< iterations; i++) { UserBeforeFieldInit._name = "oec2003"; } Console.WriteLine("Test1-UserBeforeFieldInit 用时:" + sw.Elapsed); sw = Stopwatch.StartNew(); for (Int32 j = 0; j < iterations; j++) { UserPrecise._name = "oec2003"; } Console.WriteLine("Test1-UserPrecise 用时:" + sw.Elapsed); } private static void Test2(Int32 iterations) { Stopwatch sw = Stopwatch.StartNew(); for (Int32 i = 0; i < iterations; i++) { UserBeforeFieldInit._name = "oec2003"; } Console.WriteLine("Test2-UserBeforeFieldInit 用时:" + sw.Elapsed); sw = Stopwatch.StartNew(); for (Int32 j = 0; j < iterations; j++) { UserPrecise._name = "oec2003"; } Console.WriteLine("Test2-UserPrecise 用时:" + sw.Elapsed); } } public class UserBeforeFieldInit { public static string _name; } public class UserPrecise { public static string _name ; static UserPrecise() { _name = "oec2003"; } } CLR Via C#调用静态构造函数测试结果如下:Judging from the results of the static constructor called by CLR Via C # above, the execution speed of the BeforeFieldInit mode is still much faster, but why is the speed of the two methods about the same on the second execution? Because the JIT compiler knows that the constructor of the type has been called after * execution, the call to the constructor is not displayed on the second execution.
At this point, I believe that you have a deeper understanding of "CLR Via C # call static constructor is", might as well to actual operation it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.