博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.Net 序列化比较
阅读量:5297 次
发布时间:2019-06-14

本文共 11690 字,大约阅读时间需要 38 分钟。

序列化:

, .Net 二进制,soap,xml,.Net Json,.

其中 phpRPC  Hessian,Burlap,SilverlightSerializer都属于二进制序列化

其余为文本传输。

每次测试包含序列化和反序列化。

最后幸存的是:Binary Hessian Burlap NewtonJson SilverlightSerializer.

综合比较 二进制序列化首选 Binary,如果需要支持Silverlight那么SilverlightSerializer也是不错的选择。

如果需要文本传输,那么 NewtonJson 适合你。

 

代码是从JavaEye论坛上拿过来修改的。

 

 

 

附代码:

ExpandedBlockStart.gif
代码
using
 System;
using
 System.Collections;
using
 System.IO;
using
 System.Collections.Generic;
using
 System.Linq;
using
 System.Text;
using
 System.Runtime.Serialization.Formatters.Binary;
using
 System.Runtime.Serialization.Formatters.Soap;
using
 System.Runtime.Serialization.Json;
using
 System.Xml.Serialization;
using
 org.phprpc.util;
using
 Newtonsoft.Json;
using
 hessiancsharp.io;
using
 burlapcsharp.io;
using
 Serialization;
namespace
 Test {
    [Serializable]
    
class
 TestClass2
    {
        
public
 
int
 ID;
        
public
 
double
 @double { 
get
set
; }
        
public
 
bool
 @bool { 
get
set
; }
        
public
 
float
 @float { 
get
set
; }
        
public
 
double
?
 @double2 { 
get
set
; }
        
public
 
bool
?
 @bool2 { 
get
set
; }
        
public
 
float
?
 @float2 { 
get
set
; }
        
public
 String UserName { 
get
set
; }
        
public
 String Password { 
get
set
; }
        
public
 DateTime Birthday { 
get
set
; }
    }
    [Serializable]
    
class
 TestClass {
        
public
 IDictionary dictionary { 
get
set
; }
        
public
 IList list { 
get
set
; }
        
//
private int[] array = new int[] { 1, 2, 3 };
        
//
public int[] Array { get { return array; } set { } }
        
public
 
int
 ID;
        
public
 
double
 @double { 
get
set
; }
        
public
 
bool
 @bool { 
get
set
; }
        
public
 
float
 @float { 
get
set
; }
        
public
 
double
?
 @double2 { 
get
set
; }
        
public
 
bool
?
 @bool2 { 
get
set
; }
        
public
 
float
?
 @float2 { 
get
set
; }
        
public
 String UserName { 
get
set
; }
        
public
 String Password { 
get
set
; }
        
public
 DateTime Birthday { 
get
set
; }
        
public
 TestClass2 TestClass2 { 
get
set
; }
    }
    
class
 Program {
        
static
 
void
 RunTest(
int
 time, Object data, String info, String filename) {
            GC.Collect();
            DateTime start, end;
            
long
 size 
=
 
0
;
            StringBuilder sb 
=
 
new
 StringBuilder();
            StringBuilder categories 
=
 
new
 StringBuilder();
            StringBuilder dataset1 
=
 
new
 StringBuilder();
            StringBuilder dataset2 
=
 
new
 StringBuilder();
            Stream stream;
            
object
 result 
=
 
null
;
            sb.Append(
"
<chart caption='
"
 
+
 time 
+
 
"
"
 
+
 info 
+
 
"
序列化与反序列化的比较' shownames='1' showvalues='1' decimals='0' formatNumberScale='0' baseFont='Tahama' baseFontSize='12'>
"
);
            categories.Append(
"
<categories>
"
);
            dataset1.Append(
"
<dataset seriesName='时间'>
"
);
            dataset2.Append(
"
<dataset seriesName='空间'>
"
);
            System.Console.WriteLine(
"
--------------------------------------------------------------------------------
"
);
            System.Console.WriteLine(time 
+
 
"
"
 
+
 info);
            
try
            {
                start 
=
 DateTime.Now;
                PHPFormatter formator1 
=
 
new
 PHPFormatter();
                
for
 (
int
 i 
=
 
0
; i 
<
 time; i
++
)
                {
                    stream 
=
 
new
 MemoryStream();
                    formator1.Serialize(stream, data);
                    size 
=
 stream.Length;
                    stream.Position 
=
 
0
;
                    result 
=
 formator1.Deserialize(stream);
                    stream.Close();
                }
                end 
=
 DateTime.Now;
                categories.Append(
"
<category label='PHP' />
"
);
                dataset1.Append(
"
<set value='
"
 
+
 ((end.Ticks 
-
 start.Ticks) 
/
 
10000
+
 
"
'/>
"
);
                dataset2.Append(
"
<set value='
"
 
+
 size 
+
 
"
'/>
"
);
                System.Console.WriteLine(
"
PHP - 
"
 
+
 
"
\t\t时间:
"
 
+
 (end.Ticks 
-
 start.Ticks) 
+
 
"
\t 长度:
"
 
+
 size);
            }
            
catch
            {
                System.Console.WriteLine(
"
PHP不支持!
"
);
            }
            GC.Collect();
            
try
            {
                start 
=
 DateTime.Now;
                BinaryFormatter formator2 
=
 
new
 BinaryFormatter();
                
for
 (
int
 i 
=
 
0
; i 
<
 time; i
++
)
                {
                    stream 
=
 
new
 MemoryStream();
                    formator2.Serialize(stream, data);
                    size 
=
 stream.Length;
                    stream.Position 
=
 
0
;
                    result 
=
 formator2.Deserialize(stream);
                    stream.Close();
                }
                end 
=
 DateTime.Now;
                categories.Append(
"
<category label='Binary' />
"
);
                dataset1.Append(
"
<set value='
"
 
+
 ((end.Ticks 
-
 start.Ticks) 
/
 
10000
+
 
"
'/>
"
);
                dataset2.Append(
"
<set value='
"
 
+
 size 
+
 
"
'/>
"
);
                System.Console.WriteLine(
"
Binary - 
"
 
+
 
"
\t时间:
"
 
+
 (end.Ticks 
-
 start.Ticks) 
+
 
"
\t 长度:
"
 
+
 size);
            }
            
catch
            {
                System.Console.WriteLine(
"
Binary不支持!
"
);
            }
            
            GC.Collect();
            
            
try
            {
                start 
=
 DateTime.Now;
                SoapFormatter formator3 
=
 
new
 SoapFormatter();
                
for
 (
int
 i 
=
 
0
; i 
<
 time; i
++
) {
                    stream 
=
 
new
 MemoryStream();
                    formator3.Serialize(stream, data);
                    size 
=
 stream.Length;
                    stream.Position 
=
 
0
;
                    result 
=
 formator3.Deserialize(stream);
                    stream.Close();
                }
                end 
=
 DateTime.Now;
                categories.Append(
"
<category label='SOAP' />
"
);
                dataset1.Append(
"
<set value='
"
 
+
 ((end.Ticks 
-
 start.Ticks) 
/
 
10000
+
 
"
'/>
"
);
                dataset2.Append(
"
<set value='
"
 
+
 size 
+
 
"
'/>
"
);
                System.Console.WriteLine(
"
SOAP - 
"
 
+
 
"
\t\t时间:
"
 
+
 (end.Ticks 
-
 start.Ticks) 
+
 
"
\t 长度:
"
 
+
 size);
            }
            
catch
 {
                System.Console.WriteLine(
"
SOAP不支持!
"
);
            }
        
            GC.Collect();
            
try
 {
                start 
=
 DateTime.Now;
                XmlSerializer formator4 
=
 
new
 XmlSerializer(data.GetType());
                
for
 (
int
 i 
=
 
0
; i 
<
 time; i
++
) {
                    stream 
=
 
new
 MemoryStream();
                    formator4.Serialize(stream, data);
                    size 
=
 stream.Length;
                    stream.Position 
=
 
0
;
                    result 
=
 formator4.Deserialize(stream);
                    stream.Close();
                }
                end 
=
 DateTime.Now;
                categories.Append(
"
<category label='XML' />
"
);
                dataset1.Append(
"
<set value='
"
 
+
 ((end.Ticks 
-
 start.Ticks) 
/
 
10000
+
 
"
'/>
"
);
                dataset2.Append(
"
<set value='
"
 
+
 size 
+
 
"
'/>
"
);
                System.Console.WriteLine(
"
XML - 
"
 
+
 
"
\t\t时间:
"
 
+
 (end.Ticks 
-
 start.Ticks) 
+
 
"
\t 长度:
"
 
+
 size);
            }
            
catch
 {
                System.Console.WriteLine(
"
XML不支持!
"
);
            }
            GC.Collect();
            
try
 {
                start 
=
 DateTime.Now;
                DataContractJsonSerializer formator5 
=
 
new
 DataContractJsonSerializer(data.GetType());
                
for
 (
int
 i 
=
 
0
; i 
<
 time; i
++
) {
                    stream 
=
 
new
 MemoryStream();
                    formator5.WriteObject(stream, data);
                    size 
=
 stream.Length;
                    stream.Position 
=
 
0
;
                    result 
=
 formator5.ReadObject(stream);
                    stream.Close();
                }
                end 
=
 DateTime.Now;
                categories.Append(
"
<category label='JSON' />
"
);
                dataset1.Append(
"
<set value='
"
 
+
 ((end.Ticks 
-
 start.Ticks) 
/
 
10000
+
 
"
'/>
"
);
                dataset2.Append(
"
<set value='
"
 
+
 size 
+
 
"
'/>
"
);
                System.Console.WriteLine(
"
JSON - 
"
 
+
 
"
\t\t时间:
"
 
+
 (end.Ticks 
-
 start.Ticks) 
+
 
"
\t 长度:
"
 
+
 size);
            }
            
catch
 {
                System.Console.WriteLine(
"
JSON不支持!
"
);
            }
            GC.Collect();
            
try
 {
                start 
=
 DateTime.Now;
                
for
 (
int
 i 
=
 
0
; i 
<
 time; i
++
) {
                    stream 
=
 
new
 MemoryStream();
                    CHessianOutput formator6o 
=
 
new
 CHessianOutput(stream);
                    formator6o.WriteObject(data);
                    size 
=
 stream.Length;
                    stream.Position 
=
 
0
;
                    CHessianInput formator6i 
=
 
new
 CHessianInput(stream);
                    result 
=
 formator6i.ReadObject();
                    stream.Close();
                }
                end 
=
 DateTime.Now;
                categories.Append(
"
<category label='Hessian' />
"
);
                dataset1.Append(
"
<set value='
"
 
+
 ((end.Ticks 
-
 start.Ticks) 
/
 
10000
+
 
"
'/>
"
);
                dataset2.Append(
"
<set value='
"
 
+
 size 
+
 
"
'/>
"
);
                System.Console.WriteLine(
"
Hessian - 
"
 
+
 
"
\t时间:
"
 
+
 (end.Ticks 
-
 start.Ticks) 
+
 
"
\t 长度:
"
 
+
 size);
            }
            
catch
 {
                System.Console.WriteLine(
"
Hessian不支持!
"
);
            }
            GC.Collect();
            
try
 {
                start 
=
 DateTime.Now;
                
for
 (
int
 i 
=
 
0
; i 
<
 time; i
++
) {
                    stream 
=
 
new
 MemoryStream();
                    CBurlapOutput formator7o 
=
 
new
 CBurlapOutput(stream);
                    formator7o.WriteObject(data);
                    size 
=
 stream.Length;
                    stream.Position 
=
 
0
;
                    CBurlapInput formator7i 
=
 
new
 CBurlapInput(stream);
                    result 
=
 formator7i.ReadObject();
                    stream.Close();
                }
                end 
=
 DateTime.Now;
                categories.Append(
"
<category label='Burlap' />
"
);
                dataset1.Append(
"
<set value='
"
 
+
 ((end.Ticks 
-
 start.Ticks) 
/
 
10000
+
 
"
'/>
"
);
                dataset2.Append(
"
<set value='
"
 
+
 size 
+
 
"
'/>
"
);
                System.Console.WriteLine(
"
Burlap - 
"
 
+
 
"
\t时间:
"
 
+
 (end.Ticks 
-
 start.Ticks) 
+
 
"
\t 长度:
"
 
+
 size);
            }
            
catch
 {
                System.Console.WriteLine(
"
Burlap不支持!
"
);
            }
            GC.Collect();
            
try
            {
                start 
=
 DateTime.Now;
                
for
 (
int
 i 
=
 
0
; i 
<
 time; i
++
)
                {
                    
string
 sstream 
=
 JsonConvert.SerializeObject(data);
                    
//
File.WriteAllText(@"E:\NewtonJS.txt", sstream);
                    size 
=
 sstream.Length;
                    result 
=
 JsonConvert.DeserializeObject(sstream);
                }
                end 
=
 DateTime.Now;
                categories.Append(
"
<category label='NewtonJS' />
"
);
                dataset1.Append(
"
<set value='
"
 
+
 ((end.Ticks 
-
 start.Ticks) 
/
 
10000
+
 
"
'/>
"
);
                dataset2.Append(
"
<set value='
"
 
+
 size 
+
 
"
'/>
"
);
                System.Console.WriteLine(
"
NewtonJS - 
"
 
+
 
"
\t时间:
"
 
+
 (end.Ticks 
-
 start.Ticks) 
+
 
"
\t 长度:
"
 
+
 size);
            }
            
catch
            {
                System.Console.WriteLine(
"
NewtonJS不支持!
"
);
            }
            
try
            {
                start 
=
 DateTime.Now;
                
for
 (
int
 i 
=
 
0
; i 
<
 time; i
++
)
                {
                    
byte
[] sstream 
=
 SilverlightSerializer.Serialize(data);
                    
//
File.WriteAllBytes(@"E:\SilverSeria.txt", sstream);
                    size 
=
 sstream.Length;
                    result 
=
 SilverlightSerializer.Deserialize(sstream);
                }
                end 
=
 DateTime.Now;
                categories.Append(
"
<category label='SilverSeria' />
"
);
                dataset1.Append(
"
<set value='
"
 
+
 ((end.Ticks 
-
 start.Ticks) 
/
 
10000
+
 
"
'/>
"
);
                dataset2.Append(
"
<set value='
"
 
+
 size 
+
 
"
'/>
"
);
                System.Console.WriteLine(
"
SilverSeria - 
"
 
+
 
"
\t时间:
"
 
+
 (end.Ticks 
-
 start.Ticks) 
+
 
"
\t 长度:
"
 
+
 size);
            }
            
catch
            {
                System.Console.WriteLine(
"
SilverSeria不支持!
"
);
            }
            categories.Append(
"
</categories>
"
);
            dataset1.Append(
"
</dataset>
"
);
            dataset2.Append(
"
</dataset>
"
);
            sb.Append(categories);
            sb.Append(dataset1);
            sb.Append(dataset2);
            sb.Append(
"
</chart>
"
);
            File.WriteAllText(filename, sb.ToString(), Encoding.Default);
            System.Console.WriteLine(result);
            System.Console.WriteLine(
"
--------------------------------------------------------------------------------
"
);
            GC.Collect();
        }
        
static
 
void
 Main(
string
[] args) {
            RunTest(
10000
1234567
"
对整数1234567
"
"
1.xml
"
);
            RunTest(
10000
true
"
对布尔值true
"
"
2.xml
"
);
            RunTest(
10000
, 1e123, 
"
对浮点数1e123
"
"
3.xml
"
);
            RunTest(
10000
1.2
"
对浮点数1.2
"
"
4.xml
"
);
            RunTest(
10000
1234567890987654321
"
对UInt64型1234567890987654321
"
"
5.xml
"
);
            RunTest(
10000
, Double.PositiveInfinity, 
"
对无穷大
"
"
6.xml
"
);
            String s 
=
 
"
PHPRPC - perfect high performance remote procedure call
"
;
            RunTest(
10000
, s, 
"
对字符串“
"
 
+
 s 
+
 
"
"
"
7.xml
"
);
            
byte
[] ba 
=
 
new
 
byte
[
10000
];
            
for
 (
int
 i 
=
 
0
; i 
<
 
10000
; i
++
)
            {
                ba[i] 
=
 (
byte
)(i 
%
 
255
);
            }
            RunTest(
2000
, ba, 
"
对10000个元素的字节数组
"
"
8.xml
"
);
            
string
[] sa 
=
 
new
 
string
[
100
];
            
for
 (
int
 i 
=
 
0
; i 
<
 
100
; i
++
)
            {
                sa[i] 
=
 s;
            }
            RunTest(
2000
, sa, 
"
对100个相同元素的字符串数组
"
"
9.xml
"
);
            sa 
=
 
new
 
string
[
100
];
            
for
 (
int
 i 
=
 
0
; i 
<
 
100
; i
++
)
            {
                sa[i] 
=
 s 
+
 i;
            }
            RunTest(
2000
, sa, 
"
对100个不同元素的字符串数组
"
"
10.xml
"
);
            Hashtable h 
=
 
new
 Hashtable();
            
for
 (
int
 i 
=
 
0
; i 
<
 
100
; i
++
)
            {
                h[s 
+
 i] 
=
 s 
+
 (i 
+
 
100
);
            }
            RunTest(
2000
, h, 
"
对索引不同内容不同具有100个字符串元素和字符串索引的 Hashtable
"
"
11.xml
"
);
            ArrayList list 
=
 
new
 ArrayList();
            
for
 (
int
 i 
=
 
0
; i 
<
 
1000
; i
++
) {
                TestClass tc 
=
 
new
 TestClass();
                tc.ID 
=
 i;
                tc.UserName 
=
 
"
PHPRPC
"
;
                tc.Password 
=
 
"
Hello
"
;
                tc.Birthday 
=
 DateTime.Now;
                tc.@float 
=
 
0
;
                tc.float2 
=
 
0
;
                TestClass2 tc2 
=
 
new
 TestClass2();
                tc2.ID 
=
 i;
                tc2.UserName 
=
 
"
PHPRPC
"
;
                tc2.Password 
=
 
"
Hello
"
;
                tc2.Birthday 
=
 DateTime.Now;
                tc2.@float 
=
 
0
;
                tc2.float2 
=
 
0
;
                tc.TestClass2 
=
 tc2;
                
try
                {
                    
//
tc.dictionary = new Hashtable();
                    tc.dictionary 
=
 
new
 Dictionary
<
string
string
>
();
                    tc.dictionary.Add(DateTime.Now.ToString(), DateTime.Now.ToString());
                    tc.dictionary.Add(DateTime.Now.ToString(), DateTime.Now.ToString());
                    tc.dictionary.Add(DateTime.Now.ToString(), DateTime.Now.ToString());
                    tc.dictionary.Add(
1
1
);
                    tc.dictionary.Add(
2
2
);
                    tc.dictionary.Add(
3
3
);
                }
                
catch
 { }
                
try
                {
                    tc.list 
=
 
new
 List
<
int
>
();
                    tc.list.Add(
1
);
                    tc.list.Add(
1
);
                    tc.list.Add(
1
);
                    tc.list.Add(
1
);
                    tc.list.Add(
1
);
                    tc.list.Add(
2
);
                    tc.list.Add(
2
);
                    tc.list.Add(
2
);
                    tc.list.Add(
3
);
                }
                
catch
 { }
                list.Add(tc);
            }
            RunTest(
1
, list, 
"
对具有1000个自定义类型元素的 ArrayList
"
"
12.xml
"
);
            System.Console.WriteLine(
"
--------------------------------------------------------------------------------
"
);
            System.Console.WriteLine(
"
----------------------------------END-------------------------------------------
"
);
           
            GC.Collect();
            Console.ReadLine();
        }
    }
}

 

posted on
2010-10-19 18:07 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/bchp/archive/2010/10/19/1855736.html

你可能感兴趣的文章
laravel
查看>>
installing the matplotlib via pip in the enviroment dos
查看>>
bzoj3312: [Usaco2013 Nov]No Change
查看>>
如何改善下面的代码 领导说了很耗资源
查看>>
Quartus II 中常见Warning 原因及解决方法
查看>>
高德地图 – 1.问题集锦
查看>>
php中的isset和empty的用法区别
查看>>
ajax VS websocket
查看>>
Android ViewPager 动画效果
查看>>
Android ijkplayer详解使用教程
查看>>
Android UI-仿微信底部导航栏布局
查看>>
Android中pendingIntent的深入理解
查看>>
Android ActionBar
查看>>
Redis集群方案Codis部署手册
查看>>
MySQL 第六天
查看>>
python 笔记一
查看>>
pip和easy_install使用方式
查看>>
数据表与简单java类(一对多的关系)
查看>>
博弈论
查看>>
CSS3 - 如何给图片增加内阴影
查看>>