原来的序列化使用微软的。可是接口、内部类无法序列化,根本没法用。
后来使用了自己的xml技术+反射,可是性能非常的垃圾。
于是用了1天时间写了个emit框架,性能大幅度提升!
对比结果:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
void
test002()
{
MyPojo2 pojo = CreateMyPojo();
StartTest( " Begin CreateObjectUsingReflection " );
for ( int i = 0 ; i < 50000 ; i ++ )
{
object content = Pixysoft.IO.XmlExporter.Instance.XmlSerialize(pojo);
}
EndTest( " Begin CreateObjectUsingReflection " );
StartTest( " Begin CreateObjectUsingReflection " );
for ( int i = 0 ; i < 50000 ; i ++ )
{
object content = XmlManager.Serialize(pojo);
}
EndTest( " Begin CreateObjectUsingReflection " );
StartTest( " Begin CreateObjectUsingReflection " );
for ( int i = 0 ; i < 50000 ; i ++ )
{
object content = XmlManager.DynamicSerialize(pojo);
}
EndTest( " Begin CreateObjectUsingReflection " );
StartTest( " Begin CreateObjectUsingReflection " );
for ( int i = 0 ; i < 50000 ; i ++ )
{
object content = DirectSerialize(pojo);
}
EndTest( " Begin CreateObjectUsingReflection " );
}
{
MyPojo2 pojo = CreateMyPojo();
StartTest( " Begin CreateObjectUsingReflection " );
for ( int i = 0 ; i < 50000 ; i ++ )
{
object content = Pixysoft.IO.XmlExporter.Instance.XmlSerialize(pojo);
}
EndTest( " Begin CreateObjectUsingReflection " );
StartTest( " Begin CreateObjectUsingReflection " );
for ( int i = 0 ; i < 50000 ; i ++ )
{
object content = XmlManager.Serialize(pojo);
}
EndTest( " Begin CreateObjectUsingReflection " );
StartTest( " Begin CreateObjectUsingReflection " );
for ( int i = 0 ; i < 50000 ; i ++ )
{
object content = XmlManager.DynamicSerialize(pojo);
}
EndTest( " Begin CreateObjectUsingReflection " );
StartTest( " Begin CreateObjectUsingReflection " );
for ( int i = 0 ; i < 50000 ; i ++ )
{
object content = DirectSerialize(pojo);
}
EndTest( " Begin CreateObjectUsingReflection " );
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
------
Test started: Assembly: Pixysoft.Framework.Configurations.dll
------
Begin CreateObjectUsingReflection
Begin CreateObjectUsingReflection
00 : 00 : 04.5937500
Begin CreateObjectUsingReflection
Begin CreateObjectUsingReflection
00 : 00 : 06.4531250
Begin CreateObjectUsingReflection
Begin CreateObjectUsingReflection
00 : 00 : 00.9218750
Begin CreateObjectUsingReflection
Begin CreateObjectUsingReflection
00 : 00 : 00.1406250
1 passed, 0 failed, 0 skipped, took 13.94 seconds (Ad hoc).
Begin CreateObjectUsingReflection
Begin CreateObjectUsingReflection
00 : 00 : 04.5937500
Begin CreateObjectUsingReflection
Begin CreateObjectUsingReflection
00 : 00 : 06.4531250
Begin CreateObjectUsingReflection
Begin CreateObjectUsingReflection
00 : 00 : 00.9218750
Begin CreateObjectUsingReflection
Begin CreateObjectUsingReflection
00 : 00 : 00.1406250
1 passed, 0 failed, 0 skipped, took 13.94 seconds (Ad hoc).
提升比率是: 7倍!!!接近一个数量级了!!!只要再优化逻辑,那么就可以达到一个数量级的提升。
顺便贴出EMIT的性能和反射比较:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
void
test005_1()
{
Person p = new Person();
p.Age = 1 ;
p.Name = " 123 " ;
p.Price = 12 ;
p.Type = PersonType.A;
StartTest( " start reflection " );
for ( int i = 0 ; i < 100000 ; i ++ )
{
ReflectionSerialize(p);
}
EndTest( " end reflection " );
StartTest( " start emit " );
DummyTypeSerializeHandler handler = DummyMethodFactory.CreateDummySerializeHandler(p.GetType());
for ( int i = 0 ; i < 100000 ; i ++ )
{
handler(p);
}
EndTest( " end emit " );
StartTest( " start emit " );
for ( int i = 0 ; i < 100000 ; i ++ )
{
ReflectionManager.CreateDummyType(p);
}
EndTest( " end emit " );
}
{
Person p = new Person();
p.Age = 1 ;
p.Name = " 123 " ;
p.Price = 12 ;
p.Type = PersonType.A;
StartTest( " start reflection " );
for ( int i = 0 ; i < 100000 ; i ++ )
{
ReflectionSerialize(p);
}
EndTest( " end reflection " );
StartTest( " start emit " );
DummyTypeSerializeHandler handler = DummyMethodFactory.CreateDummySerializeHandler(p.GetType());
for ( int i = 0 ; i < 100000 ; i ++ )
{
handler(p);
}
EndTest( " end emit " );
StartTest( " start emit " );
for ( int i = 0 ; i < 100000 ; i ++ )
{
ReflectionManager.CreateDummyType(p);
}
EndTest( " end emit " );
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
------
Test started: Assembly: Pixysoft.Framework.Reflection.dll
------
start reflection
end reflection
00 : 00 : 04.2812500
start emit
end emit
00 : 00 : 01.2343750 //内存直接调用
start emit
end emit
00 : 00 : 01.5937500 //缓存调用
1 passed, 0 failed, 0 skipped, took 7.33 seconds (Ad hoc).
start reflection
end reflection
00 : 00 : 04.2812500
start emit
end emit
00 : 00 : 01.2343750 //内存直接调用
start emit
end emit
00 : 00 : 01.5937500 //缓存调用
1 passed, 0 failed, 0 skipped, took 7.33 seconds (Ad hoc).
性能比较是: 3.47倍!
看来使用了自己的EMIT对象之后,性能提升了非常快!