title: Automapper performance
body: <p>I'm using Automapper to map my business model to a ViewModel.</p>
  
  <p>It works, but it's very slow.</p>
  
  <p>I have a collection with 6893 objects with 23 properties (test environment, production should have much more).</p>
  
  <p>With a loop it takes <code>00:02:32.8118534</code> to map everything.</p>
  
  <pre><code>var objects = // get all items (returns a collection of MyObj)
  List&lt;MyViewModel&gt; collection = new List&lt;MyViewModel&gt;();
  foreach (MyObj obj in objects)
  {
       MyViewModel vm = Mapper.Map&lt;MyObj, MyViewModel&gt;(obj);
       collection.Add(vm);
  }
  </code></pre>
  
  <p>I tried to improve it like this:</p>
  
  <pre><code>var objects = // get all items (returns a collection of MyObj)
  IEnumerable&lt;MyViewModel&gt; collection = mapper.Map&lt;IEnumerable&lt;MyObj&gt;, IEnumerable&lt;MyViewModel&gt;&gt;(objects);
  </code></pre>
  
  <p>And it took <code>00:02:25.4527961</code> to map everything.</p>
  
  <p>So it didn't help that much.</p>
  
  <p>None of my object's properties can be <code>null</code>.</p>
  
  <p>This is how I configured the mapper:</p>
  
  <pre><code>var config = new MapperConfiguration(cfg =&gt;
          {
              cfg.CreateMap&lt;MyObj, MyViewModel&gt;();
              cfg.CreateMap&lt;MyObjOtherObj, MyViewModelOtherObj&gt;();
          });
  mapper = config.CreateMapper();
  </code></pre>
  
  <p>MyObj:</p>
  
  <pre><code>public partial class MyObj
  {
      public MyObj()
      {
          this.MyObjOtherObj= new HashSet&lt;MyObjOtherObj&gt;();
      }
  
      public long a{ get; set; }
      public short b{ get; set; }
      public string c{ get; set; }
      public string d{ get; set; }
      public string e{ get; set; }
      public string f{ get; set; }
      public string g{ get; set; }
      public string h{ get; set; }
      public string i{ get; set; }
      public string j{ get; set; }
      public string k{ get; set; }
      public string l{ get; set; }
      public string m{ get; set; }
      public bool n{ get; set; }
      public bool o{ get; set; }
      public bool p{ get; set; }
      public bool q{ get; set; }
  
      public virtual ICollection&lt;MyObjOtherObj&gt; MyObjOtherObj{ get; set; }
      public virtual Types Types { get; set; }
  }
  </code></pre>
  
  <p>MyViewModel:</p>
  
  <pre><code>public class MyViewModel
  {
      public long a{ get; set; }
      public short b{ get; set; }
      public string c{ get; set; }
      public string d{ get; set; }
      public string e{ get; set; }
      public string f{ get; set; }
      public string g{ get; set; }
      public string h{ get; set; }
      public string i{ get; set; }
      public string j{ get; set; }
      public string k{ get; set; }
      public string l{ get; set; }
      public string m{ get; set; }
      public bool n{ get; set; }
      public bool o{ get; set; }
      public bool p{ get; set; }
      public bool q{ get; set; }
      public string TypesDescription { get; set; }
  
      public List&lt;MyViewModelOtherObj&gt; MyObjOtherObj { get; set; }
  }
  </code></pre>
  
  <p>MyObjOtherObj:</p>
  
  <pre><code>public partial class MyObjOtherObj
  {
      public long id{ get; set; }
      public long MyObjId { get; set; }
      public short x{ get; set; }
      public string z{ get; set; }
  
      public virtual MyObj MyObj{ get; set; }
      public virtual SourceTypes SourceTypes { get; set; }
  }
  </code></pre>
  
  <p>MyViewModelOtherObj:</p>
  
  <pre><code>public class MyViewModelOtherObj
  {
      public long Id { get; set; }
      public long MyObjId { get; set; }
      public short x{ get; set; }
      public string z{ get; set; }
      public string SourceTypesDescription { get; set; }
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>SourceTypes:</p>
  
  <pre><code>public partial class SourceTypes
  {
      public SourceTypes()
      {
          this.MyObjOtherObj = new HashSet&lt;MyObjOtherObj&gt;();
      }
  
      public short SourceTypeId { get; set; }
      public string Description { get; set; }
  
      public virtual ICollection&lt;MyObjOtherObj&gt; MyObjOtherObj { get; set; }
  }
  </code></pre>
  
  <p>Types:</p>
  
  <pre><code>public partial class Types
  {
      public Types()
      {
          this.MyObj = new HashSet&lt;MyObj&gt;();
      }
  
      public short TypeId { get; set; }
      public string Description { get; set; }
  
      public virtual ICollection&lt;MyObj&gt; MyObj{ get; set; }
  }
  </code></pre>