fork download
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. public class Person
  5. {
  6. public Person(string name, int age)
  7. {
  8. Age = age;
  9. Name = name;
  10. }
  11.  
  12. public int Age { get; set; }
  13. public string Name { get; set; }
  14. }
  15.  
  16. public class Test
  17. {
  18. public static void Main()
  19. {
  20. Stopwatch sw = new Stopwatch();
  21. sw.Start();
  22.  
  23. for (int i = 0; i < 1000000; i++)
  24. {
  25. Person p = new Person("John", 35);
  26. var age = p.Age;
  27. var name = p.Name;
  28. }
  29.  
  30. Console.WriteLine("Loop with inner allocation took {0}", sw.Elapsed);
  31.  
  32. Stopwatch sw2 = new Stopwatch();
  33. sw2.Start();
  34. Person px = new Person("John", 35);
  35.  
  36. for (int i = 0; i < 1000000; i++)
  37. {
  38. var age = px.Age;
  39. var name = px.Name;
  40. }
  41.  
  42. Console.WriteLine("Loop with outter allocation took {0}", sw2.Elapsed);
  43. }
  44. }
Success #stdin #stdout 0.09s 33992KB
stdin
Standard input is empty
stdout
Loop with inner allocation took 00:00:00.0541856
Loop with outter allocation took 00:00:00.0015612