using System;
using ProtoBuf;
namespace Proto.Sample
{
public enum TaskPriority
{
Low,
Medium,
High
}
[ Serializable ] // <-- BinaryFormatter
[ProtoContract]
public class Task
{
[ProtoMember(1)]
public int Id { get ; set ; }
[ProtoMember(2)]
public DateTime CreatedAt { get ; set ; }
[ProtoMember(3)]
public string CreatedBy { get ; set ; }
[ProtoMember(4)]
public TaskPriority Priority { get ; set ; }
[ProtoMember(5)]
public string Content { get ; set ; }
}
}
* This source code was highlighted with Source Code Highlighter .
using System;
using System.Collections. Generic ;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using ProtoBuf;
namespace Proto.Sample
{
internal class Program
{
private static void Main( string [] args)
{
var tasks = new List <Task>
{
new Task
{
Id = 1,
CreatedBy = "Steve Jobs" ,
CreatedAt = DateTime .Now,
Priority = TaskPriority.High,
Content = "Invent new iPhone"
},
new Task
{
Id = 2,
CreatedBy = "Steve Ballmer" ,
CreatedAt = DateTime .Now.AddDays(-7),
Priority = TaskPriority.Low,
Content = "Install own Skype"
}
};
Console .WriteLine( "The test of binary formatter:" );
const string file1 = "tasks1.bin" ;
TestBinaryFormatter(tasks, file1, 1000);
TestBinaryFormatter(tasks, file1, 2000);
TestBinaryFormatter(tasks, file1, 3000);
TestBinaryFormatter(tasks, file1, 4000);
TestBinaryFormatter(tasks, file1, 5000);
Console .WriteLine( "\nThe test of protobuf-net:" );
const string file2 = "tasks2.bin" ;
TestProtoBuf(tasks, file2, 1000);
TestProtoBuf(tasks, file2, 2000);
TestProtoBuf(tasks, file2, 3000);
TestProtoBuf(tasks, file2, 4000);
TestProtoBuf(tasks, file2, 5000);
Console .WriteLine( "\nThe comparision of file size:" );
Console .WriteLine( "The size of {0} is {1} bytes" , file1, ( new FileInfo(file1)).Length);
Console .WriteLine( "The size of {0} is {1} bytes" , file2, ( new FileInfo(file2)).Length);
Console .ReadKey();
}
private static void TestBinaryFormatter(IList<Task> tasks, string fileName, int iterationCount)
{
var stopwatch = new Stopwatch();
var formatter = new BinaryFormatter();
using ( var file = File .Create(fileName))
{
stopwatch.Restart();
for ( var i = 0; i < iterationCount; i++)
{
file.Position = 0;
formatter.Serialize(file, tasks);
file.Position = 0;
var restoredTasks = ( List <Task>)formatter.Deserialize(file);
}
stopwatch.Stop();
Console .WriteLine( "{0} iterations in {1} ms" , iterationCount, stopwatch.ElapsedMilliseconds);
}
}
private static void TestProtoBuf(IList<Task> tasks, string fileName, int iterationCount)
{
var stopwatch = new Stopwatch();
using ( var file = File .Create(fileName))
{
stopwatch.Restart();
for ( var i = 0; i < iterationCount; i++)
{
file.Position = 0;
Serializer.Serialize(file, tasks);
file.Position = 0;
var restoredTasks = Serializer.Deserialize< List <Task>>(file);
}
stopwatch.Stop();
Console .WriteLine( "{0} iterations in {1} ms" , iterationCount, stopwatch.ElapsedMilliseconds);
}
}
}
}
* This source code was highlighted with Source Code Highlighter .
The test of binary formatter:
1000 iterations in 423 ms
2000 iterations in 381 ms
3000 iterations in 532 ms
4000 iterations in 660 ms
5000 iterations in 814 ms
The test of protobuf-net:
1000 iterations in 1056 ms
2000 iterations in 76 ms
3000 iterations in 129 ms
4000 iterations in 152 ms
5000 iterations in 202 ms
The comparision of file size:
The size of tasks1.bin is 710 bytes
The size of tasks2.bin is 101 bytes
* This source code was highlighted with Source Code Highlighter .
var model = TypeModel.Create();
model.Add( typeof (Task), true );
var compiledModel = model.Compile(path);
compiledModel.Serialize(file, tasks);
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/119503/