Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Results on Win8.0/AMD64:
- // ReflectionCloner took 340 ms
- // ExpressionTreeCloner took 56 ms
- // SerializationCloner took 3395 ms (3.4 s)
- // Protobuf-net took 2856569 ms (48 m)
- using System;
- using System.Diagnostics;
- using System.IO;
- using ProtoBuf;
- using ProtoBuf.Meta;
- using Nuclex.Support.Cloning;
- namespace ConsoleApplication1 {
- class Program {
- public class FunkyType {
- public FunkyType Nest;
- public int SomeInt;
- public string SomeString;
- public float SomeFloat;
- }
- static void Main(string[] args) {
- var x = new FunkyType() {
- Nest = new FunkyType() {
- SomeString = "More text"
- },
- SomeInt = 123,
- SomeString = "Lotsa text",
- SomeFloat = 456.789f
- };
- MetaType stringSegmentMetaType = RuntimeTypeModel.Default.Add(
- typeof(FunkyType), applyDefaultBehaviour: false
- );
- stringSegmentMetaType
- .Add(1, "Nest")
- .Add(2, "SomeInt")
- .Add(3, "SomeString")
- .Add(4, "SomeFloat");
- FunkyType x2;
- // Reflection cloner
- {
- var w = Stopwatch.StartNew();
- for(int index = 0; index < 100000; ++index) {
- x2 = ReflectionCloner.DeepFieldClone(x);
- }
- Console.WriteLine("ReflectionCloner took " + w.ElapsedMilliseconds.ToString() + " ms");
- }
- // Expression tree cloner
- {
- var w = Stopwatch.StartNew();
- for(int index = 0; index < 100000; ++index) {
- x2 = ExpressionTreeCloner.DeepFieldClone(x);
- }
- Console.WriteLine("ExpressionTreeCloner took " + w.ElapsedMilliseconds.ToString() + " ms");
- }
- // Serialization cloner
- {
- var w = Stopwatch.StartNew();
- for(int index = 0; index < 100000; ++index) {
- x2 = SerializationCloner.DeepFieldClone(x);
- }
- Console.WriteLine("SerializationCloner took " + w.ElapsedMilliseconds.ToString() + " ms");
- }
- // Protobuf cloner
- using(var memoryStream = new MemoryStream()) {
- var uselessContext = new SerializationContext();
- var w = Stopwatch.StartNew();
- for(int index = 0; index < 100000; ++index) {
- using(
- var writer = new ProtoWriter(
- memoryStream, RuntimeTypeModel.Default, uselessContext
- )
- ) {
- RuntimeTypeModel.Default.Serialize(writer, x);
- }
- memoryStream.Position = 0;
- using(
- var reader = new ProtoReader(
- memoryStream, RuntimeTypeModel.Default, uselessContext
- )
- ) {
- x2 = (FunkyType)RuntimeTypeModel.Default.Deserialize(
- reader, null, typeof(FunkyType)
- );
- }
- }
- Console.WriteLine("Protobuf-net took " + w.ElapsedMilliseconds.ToString() + " ms");
- memoryStream.Position = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement