Skip to content

Instantly share code, notes, and snippets.

@neuecc
Created October 25, 2016 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neuecc/caf61c55e4e3dc42003fb07dc358fc5e to your computer and use it in GitHub Desktop.
Save neuecc/caf61c55e4e3dc42003fb07dc358fc5e to your computer and use it in GitHub Desktop.
SerializerPerformance
using MsgPack.Serialization;
using ProtoBuf;
using System;
using System.Diagnostics;
using System.IO;
using ZeroFormatter;
// スーパー雑雑クラス。
[ZeroFormattable]
[Serializable]
[ProtoContract]
public class Person
{
[Index(0)]
[MessagePackMember(0)]
[ProtoMember(1)]
public virtual int Age { get; set; }
[Index(1)]
[MessagePackMember(1)]
[ProtoMember(2)]
public virtual string FirstName { get; set; }
[Index(2)]
[MessagePackMember(2)]
[ProtoMember(3)]
public virtual string LastName { get; set; }
[Index(3)]
[MessagePackMember(3)]
[ProtoMember(4)]
public virtual Sex Sex { get; set; }
}
public enum Sex
{
Unknown, Male, Female,
}
class Program
{
static void Main(string[] args)
{
var p = new Person
{
Age = 99,
FirstName = "hoge",
LastName = "huga",
Sex = Sex.Male,
};
// 雑あっため。
MsgPack.Serialization.SerializationContext.Default.GetSerializer<Person>().PackSingleObject(p);
MsgPack.Serialization.SerializationContext.Default.GetSerializer<Person>().PackSingleObject(p);
MsgPack.Serialization.SerializationContext.Default.GetSerializer<Person>().PackSingleObject(p);
ZeroFormatterSerializer.Serialize(p);
ZeroFormatterSerializer.Serialize(p);
ZeroFormatterSerializer.Serialize(p);
ProtoBuf.Serializer.Serialize<Person>(new MemoryStream(), p);
ProtoBuf.Serializer.Serialize<Person>(new MemoryStream(), p);
ProtoBuf.Serializer.Serialize<Person>(new MemoryStream(), p);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
// 雑計測
var sw = Stopwatch.StartNew();
for (int i = 0; i < 10000; i++)
{
// 5msぐらい
// ZeroFormatterSerializer.Serialize(p);
// 4msぐらい
// ProtoBuf.Serializer.Serialize<Person>(new MemoryStream(), p);
// 130msぐらい。。。
// MessagePackSerializer.Get<Person>().PackSingleObject(p);
}
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalMilliseconds + "ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment