c#中的序列化与反序列化
2016-01-19 14:14
387 查看
最近想改改项目中读取文件的代码,研究了下c#中的序列化与反序列化,.net自带的性能很差啊,感觉还是针对自己的系统单写个比较快。。
转:
http://www.maxondev.com/serialization-performance-comparison-c-net-formats-frameworks-xmldatacontractserializer-xmlserializer-binaryformatter-json-newtonsoft-servicestack-text/
Hi big shot!
This time I’ll talk about performance of serialization. Serialization is
a common task we use mostly for communication and storage. This post will give a wide serialization performance comparison.
Real life scenarios- Lately a micro services architecture become very
common, in such architecture you have to provide a way for your micro services to communicate between themselves so you’ll have to serialize your objects for that. Normally you will use messaging frameworks that will do it for you but it’s essential to understand
what’s going under the hood. In other cases, you’ll need to develop a framework or a tool that will need to serialize it’s objects itself. In addition, in many communication frameworks you’re able to change the serialization method so it’s important to understand
what you’ll achieve by doing so.
In this post I won’t talk about the advantages or disadvantages of each format/framework, I’ll stick strictly with performance (Space and Speed). That said, when you
choose a format and framework you sure need to think about much more than that. Examples are ease of use, extensibility, flexibility, versioning, and much more.
Although my test run in C#, this post applies to any technology. The size obviously will be the same, and about the speed I believe that it will differ, but the ratio
between the formats will be roughly the same in most cases.
I’ll test popular formats and frameworks –
XML (XmlSerializer, DataContractSerializer) , BinaryFormatter – Are included with the
.NET framework http://msdn.microsoft.com/en-gb/vstudio/hh341490.aspx
JSON – http://james.newtonking.com/json, https://servicestack.net/text
MsgPack – https://github.com/msgpack/msgpack-cli
Protobuf – https://code.google.com/p/protobuf-net/
The way I built the testing code it’s very easy to set up new tests for other frameworks. You just need to implement the abstract methods of the SerializationTester<TTestObject> class.
So with the provided code you can easily test different serialization frameworks with different sample data that fits your needs the best. This is something you can do within minutes with my code.
So why do I need to change the serialization method in my application? In most applications you probably don’t.
However, it’s important to be aware of the differences which are significant. If your application is heavy on serialization you should consider it. It’s important to understand that we’re testing two factors –
Space which affects storage/memory (storing objects) and network usage(communication)
Speed which the time takes to serialize/deserialize the object.
So let’s begin with the comparison. To compare we need data, I’ve chosen to use a list of Belgian beers, after all – who doesn’t like beer? So by Wikipedia there’re 1610 different beers made in Belgium and they all listed on this page – http://en.wikipedia.org/wiki/List_of_Belgian_beer.
4000
Then I run the tests, and plotted them on a chart for you. Of course the code to retrieve the list and all the tests is available on GitHub –https://github.com/maximn/SerializationPerformanceTest_CSharp
How a Beer looks
like?
It’s pretty straight forward, a beer has – Brand, Brewery, Alcohol level and a List of sorts that apply to this beer.
I wanted to do the comparison for large data and small data so I used the list of all 1610 beers and one beer respectively.
This is the summary of the results (Size in bytes, time in milliseconds).
But of course I’ve added charts to make the comparison easy
(* smaller is better)
We can see huge differences between formats. Interesting to note that Binary which would be in the middle for large objects would be the worst for small objects.
Now for the speed, for easy comparison I’ve decided to normalize the results so in both charts it shows the time for 1 item (beer). For the large data (list) I just used the time took to handle the list divided by the number of items(1610). I’ve ran the test
100 times and took the average run speed.
(* smaller is better)
All the test ran on my laptop – Lenovo X1 Carbon (i7-3667U). The tests obviously gave me different results each run but what’s really interesting is the ratio between the formats.
It is important to say that I’ve used all those frameworks with their default settings. I’m sure it’s possible to tweak it a bit but I just wanted to compare their defaults.
A bit about the testing code – All testers implement SerializationTester<TTestObject> class. They implement an Init, Serialize, and Deserialize methods. The base tester will run
the tests 100 times and will output the results to the console. All the tests measure in memory serialization/deserialization so hard disk speed doesn’t affect the results.
So which one should you use? I don’t really have an answer for that one. It all depends on your needs, but this post intends to help you make the right decision.
转:
http://www.maxondev.com/serialization-performance-comparison-c-net-formats-frameworks-xmldatacontractserializer-xmlserializer-binaryformatter-json-newtonsoft-servicestack-text/
Serialization Performance comparison (C#/.NET) – Formats & Frameworks (XML–DataContractSerializer & XmlSerializer, BinaryFormatter, JSON– Newtonsoft & ServiceStack.Text, Protobuf, MsgPack)
Hi big shot!This time I’ll talk about performance of serialization. Serialization is
a common task we use mostly for communication and storage. This post will give a wide serialization performance comparison.
Real life scenarios- Lately a micro services architecture become very
common, in such architecture you have to provide a way for your micro services to communicate between themselves so you’ll have to serialize your objects for that. Normally you will use messaging frameworks that will do it for you but it’s essential to understand
what’s going under the hood. In other cases, you’ll need to develop a framework or a tool that will need to serialize it’s objects itself. In addition, in many communication frameworks you’re able to change the serialization method so it’s important to understand
what you’ll achieve by doing so.
In this post I won’t talk about the advantages or disadvantages of each format/framework, I’ll stick strictly with performance (Space and Speed). That said, when you
choose a format and framework you sure need to think about much more than that. Examples are ease of use, extensibility, flexibility, versioning, and much more.
Although my test run in C#, this post applies to any technology. The size obviously will be the same, and about the speed I believe that it will differ, but the ratio
between the formats will be roughly the same in most cases.
I’ll test popular formats and frameworks –
XML (XmlSerializer, DataContractSerializer) , BinaryFormatter – Are included with the
.NET framework http://msdn.microsoft.com/en-gb/vstudio/hh341490.aspx
JSON – http://james.newtonking.com/json, https://servicestack.net/text
MsgPack – https://github.com/msgpack/msgpack-cli
Protobuf – https://code.google.com/p/protobuf-net/
The way I built the testing code it’s very easy to set up new tests for other frameworks. You just need to implement the abstract methods of the SerializationTester<TTestObject> class.
So with the provided code you can easily test different serialization frameworks with different sample data that fits your needs the best. This is something you can do within minutes with my code.
So why do I need to change the serialization method in my application? In most applications you probably don’t.
However, it’s important to be aware of the differences which are significant. If your application is heavy on serialization you should consider it. It’s important to understand that we’re testing two factors –
Space which affects storage/memory (storing objects) and network usage(communication)
Speed which the time takes to serialize/deserialize the object.
So let’s begin with the comparison. To compare we need data, I’ve chosen to use a list of Belgian beers, after all – who doesn’t like beer? So by Wikipedia there’re 1610 different beers made in Belgium and they all listed on this page – http://en.wikipedia.org/wiki/List_of_Belgian_beer.
4000
Then I run the tests, and plotted them on a chart for you. Of course the code to retrieve the list and all the tests is available on GitHub –https://github.com/maximn/SerializationPerformanceTest_CSharp
How a Beer looks
like?
It’s pretty straight forward, a beer has – Brand, Brewery, Alcohol level and a List of sorts that apply to this beer.
I wanted to do the comparison for large data and small data so I used the list of all 1610 beers and one beer respectively.
This is the summary of the results (Size in bytes, time in milliseconds).
But of course I’ve added charts to make the comparison easy
(* smaller is better)
We can see huge differences between formats. Interesting to note that Binary which would be in the middle for large objects would be the worst for small objects.
Now for the speed, for easy comparison I’ve decided to normalize the results so in both charts it shows the time for 1 item (beer). For the large data (list) I just used the time took to handle the list divided by the number of items(1610). I’ve ran the test
100 times and took the average run speed.
(* smaller is better)
All the test ran on my laptop – Lenovo X1 Carbon (i7-3667U). The tests obviously gave me different results each run but what’s really interesting is the ratio between the formats.
It is important to say that I’ve used all those frameworks with their default settings. I’m sure it’s possible to tweak it a bit but I just wanted to compare their defaults.
A bit about the testing code – All testers implement SerializationTester<TTestObject> class. They implement an Init, Serialize, and Deserialize methods. The base tester will run
the tests 100 times and will output the results to the console. All the tests measure in memory serialization/deserialization so hard disk speed doesn’t affect the results.
So which one should you use? I don’t really have an answer for that one. It all depends on your needs, but this post intends to help you make the right decision.
Share this:
相关文章推荐
- C#获取显示器宽度高度,桌面宽度高度等
- C#异步:实现一个最简单的异步
- C#事件
- C# 单击按钮打开文件对话框
- C#多线程(Thread)开发基础
- c#常用描述统计方法总结
- C#及wpf WebBrowser截图
- C# 打开TXT文件读取内容
- 关于C# this 指针
- c#事务
- C# double 四舍五入
- C#读取歌曲列表获取单个音乐文件信息
- [C#].NET中的三种Timer的区别和用法
- C#递归扫描文件夹中的文件
- C# 向ListView中添加多列数据的方法
- C#索引器的详细用法
- c# 两个数组比较,将重复部分去掉,返回不重复部分
- C# -- 委托的介绍(delegate、Action、Func、predicate)
- C# 队列,
- C#日期格式化