More details:
I used the release build in the test (not the debug build as it very often gives very wrong results)
Software used in the test:
- Windows XP Pro SP2
- .NET Framework 2.0
The specs of my computer (where the test was done) are:
- CPU Intel 1.6GHz Dual Core
- 1024 MB RAM
Used Code
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace singleton_vs_nested_classes { class Program { static void Main(string[] args) { const int LOOP_COUNT = 1000000000; int tempVar = 0; Thread.Sleep(3000); // TestStatic // tempVar = TestStatic.TestVar; DateTime beforeTestStatic = DateTime.Now; for (int i = 0; i < LOOP_COUNT; i++) { tempVar = TestStatic.TestVar; } DateTime afterTestStatic = DateTime.Now; // TestStaticWithTypeInitializer // tempVar = TestStaticWithTypeInitializer.TestVar; DateTime beforeTestStaticWithTypeInitializer = DateTime.Now; for (int i = 0; i < LOOP_COUNT; i++) { tempVar = TestStaticWithTypeInitializer.TestVar; } DateTime afterTestStaticWithTypeInitializer = DateTime.Now; // TestNested // tempVar = TestNested.MyNestedClass.TestVar; DateTime beforeTestNested = DateTime.Now; for (int i = 0; i < LOOP_COUNT; i++) { tempVar = TestNested.MyNestedClass.TestVar; } DateTime afterTestNested = DateTime.Now; // TestSingltonLazyLoading // tempVar = TestSingltonLazyLoading.Singlton.TestVar; DateTime beforeTestSingltonLazyLoading = DateTime.Now; for (int i = 0; i < LOOP_COUNT; i++) { tempVar = TestSingltonLazyLoading.Singlton.TestVar; } DateTime afterTestSingltonLazyLoading = DateTime.Now; // TestSingltonInitInTypeInitliazer // tempVar = TestSingltonInitInTypeInitliazer.Singlton.TestVar; DateTime beforeTestSingltonInitInTypeInitliazer = DateTime.Now; for (int i = 0; i < LOOP_COUNT; i++) { tempVar = TestSingltonInitInTypeInitliazer.Singlton.TestVar; } DateTime afterTestSingltonInitInTypeInitliazer = DateTime.Now; // Show results // Console.WriteLine("{0} TestStatic", afterTestStatic.Subtract(beforeTestStatic)); Console.WriteLine("{0} TestStaticWithTypeInitializer", afterTestStaticWithTypeInitializer.Subtract(beforeTestStaticWithTypeInitializer)); Console.WriteLine("{0} TestNested", afterTestNested.Subtract(beforeTestNested)); Console.WriteLine("{0} TestSingltonLazyLoading", afterTestSingltonLazyLoading.Subtract(beforeTestSingltonLazyLoading)); Console.WriteLine("{0} TestSingltonInitInTypeInitliazer", afterTestSingltonInitInTypeInitliazer.Subtract(beforeTestSingltonInitInTypeInitliazer)); Console.Read(); } } public static class TestStatic { private static int _testVar = 5; public static int TestVar { get { return _testVar; } } } public static class TestStaticWithTypeInitializer { static TestStaticWithTypeInitializer() { _testVar = 5; } private static int _testVar; public static int TestVar { get { return _testVar; } } } public static class TestNested { public static class MyNestedClass { private static int _testVar = 5; public static int TestVar { get { return _testVar; } } } } public static class TestSingltonLazyLoading { private static TestSinglton _singltonInstance; public static TestSinglton Singlton { get { if (_singltonInstance == null) { _singltonInstance = new TestSinglton(5); } return _singltonInstance; } } } public static class TestSingltonInitInTypeInitliazer { static TestSingltonInitInTypeInitliazer() { _singltonInstance = new TestSinglton(5); } private static TestSinglton _singltonInstance; public static TestSinglton Singlton { get { return _singltonInstance; } } } public class TestSinglton { public TestSinglton(int testVar) { _testVar = testVar; } private int _testVar; public int TestVar { get { return _testVar; } } } }Results: 00:00:01.2656250 TestStatic
00:00:12.8437500 TestStaticWithTypeInitializer
00:00:01.2812500 TestNested
00:00:03.2187500 TestSingltonLazyLoading
00:00:09.3125000 TestSingltonInitInTypeInitliazer
People should read this.
ReplyDelete