entity2.String1 = \"String 1 in te2\";
entity2.Int1 = 999;
entity2.Datetime1 = new DateTime(2009, 12, 1);
int loops = 10000;
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < loops; i++)
{
EmitTest.GetValueReflect(typeof(TestEntity), \"String1\", entity1);
EmitTest.GetValueReflect(typeof(TestEntity), \"Int1\", entity1);
EmitTest.GetValueReflect(typeof(TestEntity), \"Datetime1\", entity1);
EmitTest.GetValueReflect(typeof(TestEntity), \"String1\", entity2);
EmitTest.GetValueReflect(typeof(TestEntity), \"Int1\", entity2);
EmitTest.GetValueReflect(typeof(TestEntity), \"Datetime1\", entity2);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < loops; i++)
{
EmitTest.GetValueEmit(typeof(TestEntity), \"String1\", entity1);
EmitTest.GetValueEmit(typeof(TestEntity), \"Int1\", entity1);
EmitTest.GetValueEmit(typeof(TestEntity), \"Datetime1\", entity1);
EmitTest.GetValueEmit(typeof(TestEntity), \"String1\", entity2);
EmitTest.GetValueEmit(typeof(TestEntity), \"Int1\", entity2);
EmitTest.GetValueEmit(typeof(TestEntity), \"Datetime1\", entity2); }
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < loops; i++)
{
EmitTest.GetValueEmitWithCache(typeof(TestEntity), \"String1\", entity1);
EmitTest.GetValueEmitWithCache(typeof(TestEntity), \"Int1\", entity1);
EmitTest.GetValueEmitWithCache(typeof(TestEntity), \"Datetime1\", entity1);
EmitTest.GetValueEmitWithCache(typeof(TestEntity), \"String1\", entity2);
EmitTest.GetValueEmitWithCache(typeof(TestEntity), \"Int1\", entity2);
EmitTest.GetValueEmitWithCache(typeof(TestEntity), \"Datetime1\", entity2);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
结果大致如下:
470
31668
32
这个测试情况下,使用Emit性能大致提高10多倍。
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/