title: Performance of Compiled Expression Tree
body: <p>I have a simple scenario, where i am trying to test the performance of a expression compiled tree on an list of stock objects. Below is the code</p>
  
  <p>The performance of expression compiled tree is 5x slower than static lambda call. I am not sure whether this is a standard performance one can expect with expression compiled tree.  Would appreciate any insight.</p>
  
  <pre><code>LambdaExpression();
  List&lt;Stock&gt; stocks = new List&lt;Stock&gt;();
  for (int ctr = 0; ctr &lt;= 5000000; ctr++)
  {
      Stock stk1 = new Stock() { Price = ctr, Symbol = "A", CloseDate = DateTime.Now, FaceValue = ctr } ;
      stocks.Add(stk1);
  }
  CompileTimeLamda(a);
  DynamicLambda(a);
  
  
  public static void LambdaExpression()
  {
      ParameterExpression CS1 = Expression.Parameter(typeof(Stock), "d");
  
      var line1 = Expression.Equal(Expression.Property(CS1, typeof(Stock).GetProperty("Symbol")), Expression.Constant("MSFT", typeof(string)));
      var line2 = Expression.GreaterThan(Expression.Property(Expression.Property(CS1, typeof(Stock).GetProperty("CloseDate")),typeof(DateTime).GetProperty("Millisecond")), 
                                   Expression.Constant(0, typeof(int)));
      var line3 = Expression.GreaterThan(Expression.Property(CS1, typeof(Stock).GetProperty("Price")), Expression.Constant((double)0, typeof(double)));
      var line4 = Expression.And(line1,line2);
      var line5 = Expression.OrElse(line4, line3);
  
      func = Expression.Lambda&lt;Func&lt;Stock, bool&gt;&gt;(line5, new ParameterExpression[] {  CS1 } ).Compile();
  }
  
  
  public static void DynamicLambda(List&lt;Stock&gt; stks)
  {
      Stopwatch watch = new Stopwatch();
      watch.Start();
      foreach (var d in stks)
      {
          func(d);
      }
      watch.Stop();
      Console.WriteLine("Dynamic Lambda :" + watch.ElapsedMilliseconds);
  }
  
  public static void CompileTimeLamda(List&lt;Stock&gt; stks)
  {
      Stopwatch watch = new Stopwatch();
      watch.Start();
      foreach (var d in stks)
      {
          if (d.Symbol == "MSFT" &amp;&amp; d.CloseDate.Millisecond &gt; 0 ||
                                    (d.Price) &gt; 0) ;
      }
      watch.Stop();
      Console.WriteLine("Compile Time Lamda " +watch.ElapsedMilliseconds);
  }
  </code></pre>