Saya memberanikan diri memberi makan kelas Anda ke generator CGbR .Karena masih tahap awal belum support DateTime
, jadi saya ganti saja dengan long. Kode serialisasi yang dihasilkan terlihat seperti ini:
public int Size
{
get
{
var size = 24;
size += Cts == null ? 0 : Cts.Count * 4;
size += Tes == null ? 0 : Tes.Count * 4;
size += Code == null ? 0 : Code.Length;
size += Message == null ? 0 : Message.Length;
return size;
}
}
public byte[] ToBytes(byte[] bytes, ref int index)
{
if (index + Size > bytes.Length)
throw new ArgumentOutOfRangeException("index", "Object does not fit in array");
GeneratorByteConverter.Include((ushort)(Cts == null ? 0 : Cts.Count), bytes, ref index);
if (Cts != null)
{
for(var i = 0; i < Cts.Count; i++)
{
var value = Cts[i];
value.ToBytes(bytes, ref index);
}
}
GeneratorByteConverter.Include((ushort)(Tes == null ? 0 : Tes.Count), bytes, ref index);
if (Tes != null)
{
for(var i = 0; i < Tes.Count; i++)
{
var value = Tes[i];
value.ToBytes(bytes, ref index);
}
}
GeneratorByteConverter.Include(Code, bytes, ref index);
GeneratorByteConverter.Include(Message, bytes, ref index);
GeneratorByteConverter.Include(StartDate.ToBinary(), bytes, ref index);
GeneratorByteConverter.Include(EndDate.ToBinary(), bytes, ref index);
return bytes;
}
public Td FromBytes(byte[] bytes, ref int index)
{
var ctsLength = GeneratorByteConverter.ToUInt16(bytes, ref index);
var tempCts = new List<Ct>(ctsLength);
for (var i = 0; i < ctsLength; i++)
{
var value = new Ct().FromBytes(bytes, ref index);
tempCts.Add(value);
}
Cts = tempCts;
var tesLength = GeneratorByteConverter.ToUInt16(bytes, ref index);
var tempTes = new List<Te>(tesLength);
for (var i = 0; i < tesLength; i++)
{
var value = new Te().FromBytes(bytes, ref index);
tempTes.Add(value);
}
Tes = tempTes;
Code = GeneratorByteConverter.GetString(bytes, ref index);
Message = GeneratorByteConverter.GetString(bytes, ref index);
StartDate = DateTime.FromBinary(GeneratorByteConverter.ToInt64(bytes, ref index));
EndDate = DateTime.FromBinary(GeneratorByteConverter.ToInt64(bytes, ref index));
return this;
}
Saya membuat daftar objek sampel seperti ini:
var objects = new List<Td>();
for (int i = 0; i < 1000; i++)
{
var obj = new Td
{
Message = "Hello my friend",
Code = "Some code that can be put here",
StartDate = DateTime.Now.AddDays(-7),
EndDate = DateTime.Now.AddDays(2),
Cts = new List<Ct>(),
Tes = new List<Te>()
};
for (int j = 0; j < 10; j++)
{
obj.Cts.Add(new Ct { Foo = i * j });
obj.Tes.Add(new Te { Bar = i + j });
}
objects.Add(obj);
}
Hasil di mesin saya dalam Release
pembuatan:
var watch = new Stopwatch();
watch.Start();
var bytes = BinarySerializer.SerializeMany(objects);
watch.Stop();
Ukuran: 149000 byte
Waktu: 2.059ms 3,13 md
Sunting: Dimulai dengan CGbR 0.4.3, serializer biner mendukung DateTime. Sayangnya DateTime.ToBinary
metodenya sangat lambat. Saya akan segera menggantinya dengan sesuatu yang lebih cepat.
Sunting2: Saat menggunakan UTC DateTime
dengan meminta ToUniversalTime()
kinerja dipulihkan dan jam masuk pada 1.669ms .