public void PerformanceReportWithNoArguments(int count) { Trace.WriteLine($"#{count} 次调用:"); double time = StopwatchHelper.Caculate(count, () => { ClassB b = new ClassB(); }).TotalMilliseconds; Trace.WriteLine($"‘New’耗时 {time} milliseconds"); double time2 = StopwatchHelper.Caculate(count, () => { ClassB b = CreateObjectFactory.CreateInstance <ClassB>(); }).TotalMilliseconds; Trace.WriteLine($"‘Emit 工厂’耗时 {time2} milliseconds"); double time3 = StopwatchHelper.Caculate(count, () => { ClassB b = ExpressionCreateObject.CreateInstance <ClassB>(); }).TotalMilliseconds; Trace.WriteLine($"‘Expression’耗时 {time3} milliseconds"); double time4 = StopwatchHelper.Caculate(count, () => { ClassB b = ExpressionCreateObjectFactory.CreateInstance <ClassB>(); }).TotalMilliseconds; Trace.WriteLine($"‘Expression 工厂’耗时 {time4} milliseconds"); double time5 = StopwatchHelper.Caculate(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 void PerformanceReport(int count) { double time = StopwatchHelper.Caculate(count, () => { ClassB b = new ClassB(); }).TotalMilliseconds; Trace.WriteLine($"‘直接实例化’{count} 次调用耗时 {time} milliseconds");//‘直接实例化’1000000 次调用耗时 23.8736 milliseconds double time2 = StopwatchHelper.Caculate(count, () => { ClassB b = CreateObjectFactory.CreateInstance <ClassB>(); }).TotalMilliseconds; Trace.WriteLine($"‘工厂创建’{count} 次调用耗时 {time2} milliseconds");//‘工厂创建’1000000 次调用耗时 149.5811 milliseconds }
public void PerformanceReportWithArguments(int count) { Trace.WriteLine($"#{count} 次调用:"); double time = StopwatchHelper.Caculate(count, () => { ClassA a = new ClassA(new ClassB()); }).TotalMilliseconds; Trace.WriteLine($"‘New’耗时 {time} milliseconds"); double time2 = StopwatchHelper.Caculate(count, () => { ClassA a = CreateObjectFactory.CreateInstance <ClassA>(new ClassB()); }).TotalMilliseconds; Trace.WriteLine($"‘Emit 工厂’耗时 {time2} milliseconds"); double time4 = StopwatchHelper.Caculate(count, () => { ClassA a = ExpressionCreateObjectFactory.CreateInstance <ClassA>(new ClassB()); }).TotalMilliseconds; Trace.WriteLine($"‘Expression 工厂’耗时 {time4} milliseconds"); double time5 = StopwatchHelper.Caculate(count, () => { ClassA a = Activator.CreateInstance(typeof(ClassA), new ClassB()) as ClassA; }).TotalMilliseconds; Trace.WriteLine($"‘Activator.CreateInstance’耗时 {time5} milliseconds"); /** #1000000 次调用: * ‘New’耗时 29.3612 milliseconds * ‘Emit 工厂’耗时 634.2714 milliseconds * ‘Expression 工厂’耗时 620.2489 milliseconds * ‘Activator.CreateInstance’耗时 588.0409 milliseconds * */ }
public void GenerateArgument() { ClassA a = CreateObjectFactory.CreateInstance <ClassA>(new ClassB()); Assert.Equal(default(int), a.GetInt()); }
public void NoArguments() { ClassB b = CreateObjectFactory.CreateInstance <ClassB>(); Assert.Equal(default(int), b.GetInt()); }