-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
192 lines (166 loc) · 5.82 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
using MsgPack.Serialization;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
namespace MessagePackVSJil
{
class Program
{
static void Main(string[] args)
{
start:
Console.WriteLine();
Console.WriteLine();
Console.Write("生成Student对象,数量:");
int count = 0;
try
{
count = Convert.ToInt32(Console.ReadLine());
}
catch {
goto start;
}
if (count > 1000000)
{
Console.WriteLine("太多了,最多只给100万");
goto start;
}
List<Student> list = new List<Student>();
for (var i = 0; i < count; i++)
{
list.Add(new Student
{
Name = $"student{i}",
Age = 12,
//Brith = DateTime.Now
});
}
//***************公平竞争修正****************************
using (StringWriter initOut = new StringWriter())
{
Jil.JSON.Serialize(list[0], initOut);
}
using (MemoryStream ms = new MemoryStream())
{
var serializer = MessagePackSerializer.Get<Student>();
serializer.Pack(ms, list[0]);
}
using (MemoryStream ms = new MemoryStream())
{
ProtoBuf.Serializer.Serialize<Student>(ms, list[0]);
}
//********************************************************
var s1 = serialize(list, "Jil JSON", data =>
{
using (StringWriter output = new StringWriter())
{
Jil.JSON.Serialize(data, output);
return ASCIIEncoding.Default.GetBytes(output.ToString());
}
});
var s = ASCIIEncoding.Default.GetString(s1);
deserialize("Jil JSON", s, (data) =>
{
using (StringReader input = new StringReader(s))
{
return Jil.JSON.Deserialize<List<Student>>(input);
}
});
var s2 = serialize(list, "BinaryFormatter", data =>
{
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, list);
return ms.ToArray();
}
});
deserialize("BinaryFormatter", s2, (data) =>
{
using (MemoryStream ms = new MemoryStream(s2))
{
BinaryFormatter formatter = new BinaryFormatter();
return formatter.Deserialize(ms) as List<Student>;
}
});
var s3 = serialize(list, "MessagePack", data =>
{
using (MemoryStream ms = new MemoryStream())
{
var serializer = MessagePackSerializer.Get<List<Student>>();
serializer.Pack(ms, list);
return ms.ToArray();
}
});
deserialize("MessagePack", s3, (data) =>
{
using (MemoryStream ms = new MemoryStream(s3))
{
var serializer = MessagePackSerializer.Get<List<Student>>();
return serializer.Unpack(ms);
}
});
var d4 = serialize(list, "ProtoBuf", data =>
{
using (MemoryStream ms = new MemoryStream())
{
ProtoBuf.Serializer.Serialize<List<Student>>(ms, data);
return ms.ToArray();
}
});
deserialize("ProtoBuf", d4,(data) =>
{
using (MemoryStream ms = new MemoryStream(data))
{
return ProtoBuf.Serializer.Deserialize<List<Student>>(ms);
}
});
while (true)
{
Console.WriteLine(" Ctrl+C to quit.");
goto start;
}
}
static byte[] serialize<T>(List<T> list,string name, Func<List<T>, byte[]> serializeFunc)
{
Console.WriteLine();
var count = list.Count();
Stopwatch watch = new Stopwatch();
watch.Start();
var bytes = serializeFunc(list);
watch.Stop();
var size = bytes.Length;
//name
Console.WriteLine($"{name}序列化{count}条记录(Student),费时:{watch.ElapsedMilliseconds}毫秒, 大小{size / 1024}kb");
return bytes;
}
static List<TDest> deserialize<TSource,TDest>(string name, TSource data,Func<TSource, List<TDest>> deserializeFunc)
{
Stopwatch watch = new Stopwatch();
watch.Start();
var list = deserializeFunc(data);
watch.Stop();
var count = list.Count();
//name
Console.WriteLine($"{name}反序列化{count}条记录(Student),费时:{watch.ElapsedMilliseconds}毫秒");
return list;
}
}
[Serializable]
[ProtoBuf.ProtoContract]
public class Student
{
[ProtoBuf.ProtoMember(1)]
public String Name { set; get; }
[ProtoBuf.ProtoMember(2)]
public int Age { set; get; }
[ProtoBuf.ProtoMember(3)]
public DateTime? Brith { set; get; }
}
}