Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance analyze ADO.NET and Entity Framework

Which one gives better performance? ADO.NET or Entity Framework.

These are the two method I want to analyze.

ADO.NET Test Method

public void ADOTest() {     Stopwatch stopwatch = Stopwatch.StartNew();     using (SqlConnection con = new SqlConnection(connection))     {         string Query = "select * from Product ";         SqlDataAdapter da = new SqlDataAdapter(Query, con);         DataSet ds = new DataSet();         con.Open();         da.Fill(ds);         DataView dv = ds.Tables[0].DefaultView;     }     stopwatch.Stop();     Console.WriteLine("ADO.NET Time Elapsed={0}", stopwatch.Elapsed); } 

Entity Framework Test Method

public void EFTest() {     Stopwatch stopwatch = Stopwatch.StartNew();     var list = _OnlineStoreEntities.Products.ToList();     stopwatch.Stop();     Console.WriteLine("Entity Framework Elapsed={0}", stopwatch.Elapsed); } 

Result in first time execution

When I ran this above method in more than 100 times. The average execution time is shown in the image:

first result

ADO.NET took only 2 milliseconds whether Entity Framework took more than 4 milliseconds.

Result in second time execution

When I ran this method again and again in single run. The average execution time between ADO.NET and EF is not much more:

second result

Question

  1. I think EF gives very worst performance in first time execution Then why we use EF?
  2. Why EF second time execution was faster than first time execution?
like image 356
Niventh Avatar asked Feb 27 '13 09:02

Niventh


People also ask

What's faster ADO.NET or Entity Framework?

In this article, we conclude that the ADO.net and the entity framework has a difference in performance as ADO.net is faster in performance, and we have also seen some key differences between them related to how they are flexible, how data provider work, the maintainable code, and also the speed of development.

Should I use ADO.NET or Entity Framework?

If we want to achieve more control over SQL commands and operations with the help of raw SQL Queries, then ADO.NET will be a great choice to start work with. Whereas if we want to develop the application in a much faster way with clear code maintainability, then Entity Framework will be the better choice.

Which technique improves the performance mostly in Entity Framework?

The AsNoTracking method tells Entity Framework to stop that additional work and so, it can improve the performance of your application. So, in theory, a query with AsNoTracking should perform better than without.

Can we use Entity Framework and ADO.NET together?

EF is built on top of ADO.Net, meaning that you can use both at the same time.


1 Answers

  1. First time EF loads metadata into memory, that takes a time. It builds in-memory representation of model from edmx file, or from source code if you are using code first. Actually EF is build at the top of ADO.NET, so it can't be faster. But it makes development much faster. And improves maintainability of your code.
  2. See 1

Take a look on msdn article Performance Considerations (Entity Framework)

like image 114
Sergey Berezovskiy Avatar answered Sep 18 '22 21:09

Sergey Berezovskiy