Is .NET 3.5's CompiledQuery slower than normal LINQ queries? Part 1

by Dominic Zukiewicz 24. March 2008 19:10

Updated: This article is 1 of the 2 posts about this problem (Problem AND Solution)

Today, I looked into using LINQ and SQL together. I read about the CompiledQuery.Compile() method which can speed up queries because of the caching. So I decided to run the queries one by one and view the performance improvement.

Unfortuantely, I witnessed that the Compiled version took over 20 times longer to run than the non-compiled. I will ask my Microsoft contacts to clarify this, but in the meantime, if you know why this is, please let me know.

Pre-requisites

  1. Visual Studio 2008
  2. Add a Linq To Sql class called Northwind.dbml
  3. Add the Customer table to the Designer.
Timings for each method
Iterations Normal Compiled
Total Query Time (s) Total Query Time (s) Notes
100 0.0156 0.2188 14 times slower
1000 0.0469 0.8906 19 times slower
5000 0.2188 4.4531 20 times slower
10000 0.4531 8.9219 20 times slower
50000 2.2500 43.9844 20 times slower
100000 4.5156 88.5938 20 times slower
500000 22.4844 442.5625 20 times slower
1000000 44.9375 886.8750 20 times slower


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Xml.Linq;
using System.Diagnostics;
using System.IO;
 
namespace ConsoleApplication1
{
   class Program
   {
       static void Main(string[] args)
       {
           Trace.Listeners.Add(new ConsoleTraceListener());
           Trace.AutoFlush = true;
 
           int individualIteration = 100000;
 
           Trace.Write("Iterations = " + individualIteration + Environment.NewLine); 
 
           using (NorthwindDataContext db = new NorthwindDataContext())
           {
               DateTime actualStart = DateTime.Now;
               double normal = 0.0;
 
               for (int i = 0; i < individualIteration; i++)
               {
                   DateTime start = DateTime.Now;
                   var q = from c in db.Customers
                           where c.City == "London"
                           select c;
 
                   normal += DateTime.Now.Subtract(start).TotalSeconds;
               }
                
               Trace.Write(String.Format("Average time per normal query = {0:F8} seconds.", Environment.NewLine, (normal / individualIteration)));
 
               Trace.Write(String.Format("Time taken for normal test = {0:F4} seconds.", Environment.NewLine + Environment.NewLine, 
               DateTime.Now.Subtract(actualStart).TotalSeconds));
         }
 
 
         double compiled = 0.0;
 
         using (NorthwindDataContext db = new NorthwindDataContext())
         {
               DateTime actualStart = DateTime.Now;
               for (int i = 0; i < individualIteration; i++)
               {
                   DateTime start = DateTime.Now;
                   var q = Queries.CustomersByCity(db, "London");
                   compiled += DateTime.Now.Subtract(start).TotalSeconds;
               }
 
               Trace.Write(String.Format("Average time per compiled query = {0:F8} seconds.",
                   Environment.NewLine, (compiled / individualIteration)));
               Trace.Write(String.Format("Time taken for compiled test = {0:F4} seconds.",
                   Environment.NewLine, DateTime.Now.Subtract(actualStart).TotalSeconds));
         }
 
         Console.Read();
     }
  }
 
  static class Queries
  {
      public static Func<NorthwindDataContext, string, IQueryable<Customer>> CustomersByCity =
          CompiledQuery.Compile((NorthwindDataContext db, string city) =>
                                 from c in db.Customers
                                 where c.City == city
                                 select c);
   }
}

Tags:

.NET 3.5

Comments

9/9/2008 5:47:25 PM #

Have you gotten any feedback from Microsoft on this test?

Pete |

5/2/2009 2:05:15 AM #

I had this same scenario on complicated linq query, where the non compiled one is pretty fast when compared to the compiled one. Have you identified the scenario in which this happens

vijai |

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar