Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public struct NoStaticCtor
- {
- private static int _myValue = 3;
- public static int GetMyValue() { return _myValue; }
- }
- public struct StaticCtor
- {
- private static int _myValue;
- public static int GetMyValue() { return _myValue; }
- static StaticCtor()
- {
- _myValue = 3;
- }
- }
- class Program
- {
- const long numTimes = 5000000000;
- static void Main(string[] args)
- {
- NoCtor1();
- WithCtor1();
- NoCtor2();
- WithCtor2();
- }
- private static void NoCtor1()
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- for (long i = 0; i < numTimes; i++)
- {
- NoStaticCtor.GetMyValue();
- }
- sw.Stop();
- Console.WriteLine("No static ctor: {0}", sw.Elapsed);
- }
- private static void NoCtor2()
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- for (long i = 0; i < numTimes; i++)
- {
- NoStaticCtor.GetMyValue();
- }
- sw.Stop();
- Console.WriteLine("No static ctor: {0}", sw.Elapsed);
- }
- private static void WithCtor1()
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- for (long i = 0; i < numTimes; i++)
- {
- StaticCtor.GetMyValue();
- }
- sw.Stop();
- Console.WriteLine("with static ctor: {0}", sw.Elapsed);
- }
- private static void WithCtor2()
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- for (long i = 0; i < numTimes; i++)
- {
- StaticCtor.GetMyValue();
- }
- sw.Stop();
- Console.WriteLine("with static ctor: {0}", sw.Elapsed);
- }
- }
Add Comment
Please, Sign In to add comment