3 回答
TA贡献1880条经验 获得超4个赞
我已经修改了@Marc的基准测试源代码,并为ServiceStack的JSV和JSON序列化器添加了结果。这是我的3yo iMac上的结果:
BinaryFormatter
Length: 1313
Serialize: 3959
Deserialize: 3395
XmlSerializer
Length: 1049
Serialize: 1710
Deserialize: 2716
DataContractSerializer
Length: 911
Serialize: 712
Deserialize: 2117
NetDataContractSerializer
Length: 1138
Serialize: 1093
Deserialize: 4825
TypeSerializer
Length: 431
Serialize: 496
Deserialize: 887
JsonSerializer
Length: 507
Serialize: 558
Deserialize: 1213
这是我添加到上述@Marc基准中的源代码。
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
var sbJsv = new StringBuilder(4096);
using (var sw = new StringWriter(sbJsv))
{
Console.WriteLine();
Console.WriteLine(typeof(TypeSerializer).Name);
TypeSerializer.SerializeToWriter(orig, sw);
var jsv = sbJsv.ToString();
Console.WriteLine("Length: " + sbJsv.Length);
TypeSerializer.DeserializeFromString<Game>(jsv);
var watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++)
{
sbJsv.Length = 0;
TypeSerializer.SerializeToWriter(orig, sw);
}
watch.Stop();
Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);
watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++)
{
TypeSerializer.DeserializeFromString<Game>(jsv);
}
watch.Stop();
Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);
}
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
var sbJson = new StringBuilder(4096);
using (var sw = new StringWriter(sbJson))
{
Console.WriteLine();
Console.WriteLine(typeof(JsonSerializer).Name);
JsonSerializer.SerializeToWriter(orig, sw);
var json = sbJson.ToString();
Console.WriteLine("Length: " + sbJson.Length);
JsonSerializer.DeserializeFromString<Game>(json);
var watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++)
{
sbJson.Length = 0;
JsonSerializer.SerializeToWriter(orig, sw);
}
watch.Stop();
Console.WriteLine("Serialize: " + watch.ElapsedMilliseconds);
watch = Stopwatch.StartNew();
for (int i = 0; i < LOOP; i++)
{
JsonSerializer.DeserializeFromString<Game>(json);
}
watch.Stop();
Console.WriteLine("Deserialize: " + watch.ElapsedMilliseconds);
}
注意:我无法获得他使用的@Marc的protobuf-net v2 r352 dll,因此我不得不将protobuf-net基准测试注释掉。
- 3 回答
- 0 关注
- 628 浏览
添加回答
举报