In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the performance of CLR Via C# static constructor". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the performance of the CLR Via C# static constructor is.
The 1 CLR Via C# static constructor is private, and access modifiers cannot be modified artificially.
The 2 CLR Via C # static constructor should not call the base class's static constructor because static fields are not inherited to subclasses.
3 CLR Via C# static constructor has one and only one in a type, and is unparameterized.
4 only static fields can be initialized in the CLR Via C# static constructor.
From the * point above, you can know that static constructors are all private, so you cannot display the area to make calls about when JIT will generate code to call static constructors. There are two theories. It is often called Precise and BeforeFieldInit.
L 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.
The timing of the call generated by the BeforeFieldInit JIT compiler: 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 take a look at a class that shows these two ways in CLR Via C #.
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:
Performance Analysis and Test of CLR Via C# static Constructor
Since it is mentioned above that CLR Via C # in BeforeFieldInit mode 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; iTunes +) {UserBeforeFieldInit._name = "oec2003";} Console.WriteLine ("Test1-UserBeforeFieldInit usage:" + sw.Elapsed); sw = Stopwatch.StartNew (); for (Int32 j = 0 J < iterations; jungle +) {UserPrecise._name = "oec2003";} Console.WriteLine ("Test1-UserPrecise usage:" + 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 usage:" + sw.Elapsed); sw = Stopwatch.StartNew (); for (Int32 j = 0; j < iterations) Console.WriteLine ("Test2-UserPrecise usage:" + sw.Elapsed);}} public class UserBeforeFieldInit {public static string _ name;} public class UserPrecise {public static string _ name; static UserPrecise () {_ name = "oec2003";}}
The CLR Via C# test results are as follows:
Performance Analysis and Test of CLR Via C# static Constructor
Judging from the above results, 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 you have a deeper understanding of "what the performance of the CLR Via C# static constructor is". You might as well do it in practice. 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.