Database Reference
In-Depth Information
Listing 13-20. Comparing the Performance of a Simple Compiled LINQ Query
private static void RunUncompiledQuery()
{
using (var context = new EFRecipesEntities())
{
// Explicitly disable query plan caching
var objectContext = ((IObjectContextAdapter) context).ObjectContext;
var associateNoCache = objectContext.CreateObjectSet<Associate>();
associateNoCache.EnablePlanCaching = false;
var watch = new Stopwatch();
long totalTicks = 0;
// warm things up
associateNoCache.Include(x => x.Paychecks).Where(a => a.Name.StartsWith("Karen")).ToList();
// query gets compiled each time
for (var i = 0; i < 10; i++)
{
watch.Restart();
associateNoCache.Include(x => x.Paychecks).Where(a => a.Name.StartsWith("Karen")).ToList();
watch.Stop();
totalTicks += watch.ElapsedTicks;
Console.WriteLine("Not Compiled #{0}: {1}", i, watch.ElapsedTicks);
}
Console.WriteLine("Average ticks without compiling: {0}", (totalTicks/10));
Console.WriteLine("");
}
}
private static void RunCompiledQuery()
{
using (var context = new EFRecipesEntities())
{
var watch = new Stopwatch();
long totalTicks = 0;
// warm things up
context.Associates.Include(x => x.Paychecks).Where(a => a.Name.StartsWith("Karen")).ToList();
totalTicks = 0;
for (var i = 0; i < 10; i++)
{
watch.Restart();
context.Associates.Include(x => x.Paychecks).Where(a => a.Name.StartsWith("Karen")).ToList();
watch.Stop();
totalTicks += watch.ElapsedTicks;
Console.WriteLine("Compiled #{0}: {1}", i, watch.ElapsedTicks);
}
Console.WriteLine("Average ticks with compiling: {0}", (totalTicks/10));
}
}
Search WWH ::




Custom Search