title: Why are structs slower than classes?
body: <p>I'm creating a C# console-app. I have some critical paths and thought that creating structs would be faster than creating classes since I would not need garbage collection for structs. In my test however I found the opposite.</p>
  
  <p>In the test below, I create 1000 structs and 1000 classes.</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          int iterations = 1000;
          Stopwatch sw = new Stopwatch();
  
          sw.Start();
          List&lt;Struct22&gt; structures = new List&lt;Struct22&gt;();
          for (int i = 0; i &lt; iterations; ++i)
          {
              structures.Add(new Struct22());
          }
          sw.Stop();
          Console.WriteLine($"Struct creation consumed {sw.ElapsedTicks} ticks");
  
          Stopwatch sw2 = new Stopwatch();
          sw2.Start();
          List&lt;Class33&gt; classes = new List&lt;Class33&gt;();
          for (int i = 0; i &lt; iterations; ++i)
          {
              classes.Add(new Class33());
          }
          sw2.Stop();
          Console.WriteLine($"Class creation consumed {sw2.ElapsedTicks} ticks");
  
  
          Console.ReadLine();
      }
  }
  </code></pre>
  
  <p>My classe / struct are simple:</p>
  
  <pre><code>class Class33
  {
      public int Property { get; set; }
      public int Field;
      public void Method() { }
  }
  
  struct Struct22
  {
      public int Property { get; set; }
      public int Field;
      public void Method() { }
  }
  </code></pre>
  
  <p>Results (drum roll please...)</p>
  
  <pre><code>Struct creating consuming 3038 ticks
  Class creating consuming 404 ticks
  </code></pre>
  
  <p>So the question is: <strong>Why would it take close to 10x the amount of time for a Class than it does for a Struct ?</strong></p>
  
  <p>EDIT.  I made the Program "Do something" by just assigning integers to the properties.</p>
  
  <pre><code> static void Main(string[] args)
      {
          int iterations = 10000000;
          Stopwatch sw = new Stopwatch();
  
          sw.Start();
          List&lt;Struct22&gt; structures = new List&lt;Struct22&gt;();
          for (int i = 0; i &lt; iterations; ++i)
          {
              Struct22 s = new Struct22()
              {
                  Property = 2,
                  Field = 3
              };
              structures.Add(s);
          }
          sw.Stop();
          Console.WriteLine($"Struct creating consuming {sw.ElapsedTicks} ticks");
  
          Stopwatch sw2 = new Stopwatch();
          sw2.Start();
          List&lt;Class33&gt; classes = new List&lt;Class33&gt;();
          for (int i = 0; i &lt; iterations; ++i)
          {
              Class33 c = new Class33()
              {
                  Property = 2,
                  Field = 3
              };
              classes.Add(c);
          }
          sw2.Stop();
          Console.WriteLine($"Class creating consuming {sw2.ElapsedTicks} ticks");
  
  
          Console.ReadLine();
      }
  </code></pre>
  
  <p>and the result is astounding to me.   Classes are still at least 2x but the simple fact of assigning integers had a 20x impact!</p>
  
  <pre><code>Struct creating consuming 903456 ticks
  Class creating consuming 4345929 ticks
  </code></pre>
  
  <p>EDIT: I removed references to Methods so there are no reference types in my Class or Struct: </p>
  
  <pre><code>class Class33
  {
      public int Property { get; set; }
      public int Field;
  }
  
  struct Struct22
  {
      public int Property { get; set; }
      public int Field;
  }
  </code></pre>