Advertisement
Guest User

Serialization vs Reflection

a guest
Nov 18th, 2011
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6.  
  7. namespace SerializeVsReflect
  8. {
  9.     [Serializable]
  10.     public class Person
  11.     {
  12.         public int Id;
  13.         public string FirstName;
  14.         public string LastName;
  15.         public int Age;
  16.         public decimal Weight;
  17.     }
  18.  
  19.     class Program
  20.     {
  21.         static void Main(string[] args)
  22.         {
  23.             var person = new Person();
  24.             person.Id = 10;
  25.             person.Age = 31;
  26.             person.FirstName = "James";
  27.             person.LastName = "Stuart";
  28.             person.Weight = 79;
  29.  
  30.             int iterations = 200000;
  31.             var serializationMs = Serialization(person, iterations);
  32.             var reflectionMs = Reflection(person, iterations);
  33.  
  34.             string resultMessageFormat = "{0}, {1} iterations: {2} ms";
  35.             Console.WriteLine(resultMessageFormat, "BinaryFormatter", iterations, serializationMs);
  36.             Console.WriteLine(resultMessageFormat, "Reflection", iterations, reflectionMs);
  37.             Console.WriteLine("Done");
  38.             Console.ReadLine();
  39.         }
  40.  
  41.         private static long Serialization(Person person, int iterations)
  42.         {
  43.             var stopwatch = new Stopwatch();
  44.             using (var stream = new MemoryStream())
  45.             {
  46.                 var formatter = new BinaryFormatter();
  47.                 stopwatch.Start();
  48.                 for (int i = 0; i < iterations; i++)
  49.                 {
  50.                     formatter.Serialize(stream, person);
  51.                     stream.Seek(0, SeekOrigin.Begin);
  52.                     var deserialized = (Person)formatter.Deserialize(stream);
  53.                 }
  54.  
  55.                 stopwatch.Stop();
  56.                 return stopwatch.ElapsedMilliseconds;
  57.             }
  58.         }
  59.  
  60.         private static long Reflection(Person person, int iterations)
  61.         {
  62.             var stopwatch = new Stopwatch();
  63.             stopwatch.Start();
  64.             for (int i = 0; i < iterations; i++)
  65.             {
  66.                 var newPerson = Activator.CreateInstance<Person>();
  67.                 var fields = newPerson.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
  68.                 foreach (var field in fields)
  69.                 {
  70.                     var value = field.GetValue(person);
  71.                     field.SetValue(newPerson, value);
  72.                 }
  73.             }
  74.  
  75.             stopwatch.Stop();
  76.  
  77.             return stopwatch.ElapsedMilliseconds;
  78.         }
  79.     }
  80. }
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement