2

I got .net 4.0 with Entity Framework 4.0 with Visual Studio 2012 and Windows 7, 64bit architecture, 4 cores.

I'm performing some tests on a console application with the AdventureWorks database. The thing that is bothering me is that the first time I take some products it takes much more time than the second time, I want to know why.

I know that there are some tasks that Entity Framework perform during it's first query, so I executed some extra query ahead.

For example, my main function contains the following :

using (var context = new Context())
{
   execute context.Clients.ToList();   // <- this takes something like 300 ms
}

using (var context = new Context())
{
   execute context.Products.ToList();   // <- this takes something like 200 ms
}

using (var context = new Context())
{
   execute context.Products.ToList();   // <- this takes something like 10 ms
}

So you see that the first query with all the "first time execution" things is taking 300 ms.

The second takes 200s, and the third one, which is the same like the second takes only 10ms.

So my question is : why is my third query taking such a small amount of time ? I do not use auto-compile (since there is no such in EF 4.0), or the CompiledQuery class.

Also my first invocation to Products is not the first query on the model, so all the work for view generation should be already done.

But still it takes so much time the first time and is so fast the second time I call Products. Does it cache something somewhere ?

Another thing I noticed is that when I compile it for 64 bit, it takes a ridiculous amount of time, something like one second. And when I compile it for 32 bit, it takes the amount I had given.

Can you give me some hints here ?

Thanks

Slauma
  • 175,098
  • 59
  • 401
  • 420

1 Answers1

0

Read this: Performance Considerations (Entity Framework)

There is quite a bit of work the EF has to do besides generate views.

Additionally, your first query on Products may cause your DB server to read the data into its cache. You should use SQL Server Profiler to distinguish between DB performance differences and client (EF) performance differences.

Regarding the 64 bit question, that reminded me of a previous SO question.

Community
  • 1
  • 1
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273