Но не знаю как сделать поддержку циклов в графе объекта.
Для .NET 2.0.
Используется Dynamic Method.
Прирост в скорости при десериализации приблизительно в 5 раз, при сериализации в 2 раза.
Вот результаты теста:
Для 10000 объектов
BinaryFormatter serialize - 00:00:02.5504643, stream length = 25620000
BinaryFormatter desirialize - 00:00:02.7247557
CustomFormatter serialize - 00:00:00.9539868, stream length = 11730000
CustomFormatter desirialize - 00:00:00.5176641, 5,2635593485598
или для 50000 объектов
BinaryFormatter serialize - 00:00:13.6380023, stream length = 130750000
BinaryFormatter desirialize - 00:00:14.5414160
CustomFormatter serialize - 00:00:05.7686300, stream length = 63050000
CustomFormatter desirialize - 00:00:02.7774915, 5,23544923254071
Код теста
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Chardex
{
class Program
{
static void Main(string[] args)
{
for (int k = 0; k < 10; k++)
{
CustomFormatter f = new CustomFormatter();
MemoryStream ms = new MemoryStream();
BinaryFormatter f2 = new BinaryFormatter();
MemoryStream ms2 = new MemoryStream();
SuperClass sc = new SuperClass();
int count = 10000;
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < count; i++)
{
f2.Serialize(ms2, sc);
}
sw.Stop();
Console.WriteLine("BinaryFormatter serialize - " + sw.Elapsed + ", stream length = " + ms2.Length);
ms2.Position = 0;
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
{
SuperClass sc2 = (SuperClass)f2.Deserialize(ms2);
}
sw.Stop();
Console.WriteLine("BinaryFormatter desirialize - " + sw.Elapsed);
double ticks = sw.ElapsedTicks;
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
{
f.Serialize(ms, sc);
}
sw.Stop();
Console.WriteLine("CustomFormatter serialize - " + sw.Elapsed + ", stream length = " + ms.Length);
ms.Position = 0;
sw.Reset();
sw.Start();
for (int i = 0; i < count; i++)
{
SuperClass sc2 = (SuperClass)f.Deserialize(ms);
}
sw.Stop();
Console.WriteLine("CustomFormatter desirialize - " + sw.Elapsed + ", " + ticks / (double)sw.ElapsedTicks);
Console.WriteLine();
}
}
}
[Serializable]
public class SuperClass
{
int intFld = 0;
Test testObj = new Test();
Dictionary<DateTime, Test> testDic = new Dictionary<DateTime, Test>();
int[] testArr = new int[] { 0, 1, 2, 3, 4 };
List<Guid> testList = new List<Guid>();
public SuperClass()
{
testObj.t = new Test2();
intFld = 100;
testDic[DateTime.Now] = new Test();
testDic[DateTime.Now] = new Test();
testDic[DateTime.Now] = new Test();
testDic[DateTime.Now] = new Test();
testDic[DateTime.Now] = new Test();
testList.Add(Guid.NewGuid());
testList.Add(Guid.NewGuid());
testList.Add(Guid.NewGuid());
}
}
[Serializable]
public class Test
{
private int val = 500;
private int test = 25;
private int test2 = 25;
private int tes42 = 25;
private string testStre = "lalalal";
public Test2 t;
}
[Serializable]
public class Test2
{
private int val = 500;
private int test = 25;
private int test2 = 25;
private int tes42 = 25;
private string testStre = "lalalal";
}
}
Исходники в след. сообщении