Advertisement
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
- {
- static void Main(string[] args)
- {
- long numTimes = 5000000000;
- 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);
- sw.Restart();
- for (long i = 0; i < numTimes; i++)
- {
- StaticCtor.GetMyValue();
- }
- sw.Stop();
- Console.WriteLine("with static ctor: {0}", sw.Elapsed);
- sw.Restart();
- for (long i = 0; i < numTimes; i++)
- {
- NoStaticCtor.GetMyValue();
- }
- sw.Stop();
- Console.WriteLine("No static ctor: {0}", sw.Elapsed);
- sw.Restart();
- for (long i = 0; i < numTimes; i++)
- {
- StaticCtor.GetMyValue();
- }
- sw.Stop();
- Console.WriteLine("with static ctor: {0}", sw.Elapsed);
- }
- }
- -----
- No static ctor: 00:00:03.3342359
- with static ctor: 00:00:14.6139917
- No static ctor: 00:00:03.2229995
- with static ctor: 00:00:12.9524860
- Press any key to continue . . .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement