public void PerformanceReportWithNoArguments() { int count = 1000000; Trace.WriteLine($"#{count} 次调用:"); double time = StopwatchHelper.Calculate(count, () => { ClassB b = new ClassB(); }).TotalMilliseconds; Trace.WriteLine($"‘New’耗时 {time} milliseconds"); double time2 = StopwatchHelper.Calculate(count, () => { ClassB b = CreateObjectFactory.CreateInstance2 <ClassB>(); }).TotalMilliseconds; Trace.WriteLine($"‘Emit 工厂’耗时 {time2} milliseconds"); double time3 = StopwatchHelper.Calculate(count, () => { ClassB b = ExpressionCreateObject.CreateInstance <ClassB>(); }).TotalMilliseconds; Trace.WriteLine($"‘Expression’耗时 {time3} milliseconds"); double time4 = StopwatchHelper.Calculate(count, () => { ClassB b = ExpressionCreateObjectFactory.CreateInstance <ClassB>(); }).TotalMilliseconds; Trace.WriteLine($"‘Expression 工厂’耗时 {time4} milliseconds"); double time5 = StopwatchHelper.Calculate(count, () => { ClassB b = Activator.CreateInstance <ClassB>(); //ClassB b = Activator.CreateInstance(typeof(ClassB)) as ClassB; }).TotalMilliseconds; Trace.WriteLine($"‘Activator.CreateInstance’耗时 {time5} milliseconds"); /** * #1000000 次调用: * * ‘New’耗时 21.7474 milliseconds * * ‘Emit 工厂’耗时 174.088 milliseconds * * ‘Expression’耗时 42.9405 milliseconds * * ‘Expression 工厂’耗时 162.548 milliseconds * * ‘Activator.CreateInstance’耗时 67.3712 milliseconds * * */ }
public ClassA(ClassB classB) { }
public void NoArguments() { ClassB b = CreateObjectFactory.CreateInstance <ClassB>(); Assert.AreEqual(default(int), b.GetInt()); }