Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

c# - 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?
question from:https://stackoverflow.com/questions/15107992/performance-analyze-ado-net-and-entity-framework

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...