性能分析ADO.NET和Entity Framework [英] Performance analyze ADO.NET and Entity Framework
问题描述
哪一个提供更好的性能? ADO.NET或实体框架。
这是两种方法,我想分析一下。
ADO.NET测试方法
公共无效ADOTest()
{
秒表= Stopwatch.StartNew();
使用(SqlConnection的CON =新的SqlConnection(连接))
{
字符串查询=从产品选择*;
SqlDataAdapter的大=新SqlDataAdapter的(查询,CON);
的DataSet DS =新的DataSet();
con.Open();
da.Fill(DS);
DataView的DV = ds.Tables [0] .DefaultView;
}
stopwatch.Stop();
Console.WriteLine(ADO.NET已用时间= {0},stopwatch.Elapsed);
}
Entity Framework的测试方法
公共无效EFTest()
{
秒表= Stopwatch.StartNew();
无功名单= _OnlineStoreEntities.Products.ToList();
stopwatch.Stop();
Console.WriteLine(实体框架已用= {0},stopwatch.Elapsed);
}
结果在第一次执行
当我跑了超过100次这样的上述方法。的平均执行时间显示的图像中的
ADO.NET只用了2毫秒实体框架是否花了超过4毫秒。
结果在第二次执行
当我一次又一次地跑了这个方法在单次运行。 ADO.NET和EF之间的平均执行时间并不多:
问
- 我觉得英孚给出了第一次执行最糟糕的表现那么为什么我们使用EF?
- 为什么EF第二次执行比第一次执行更快?
- 第一次加载EF元数据到内存中,这需要时间。它建立在内存中的模型表示从EDMX文件,或者从源代码,如果你第一次使用的代码。其实EF是建立在ADO.NET的顶部,因此它不能更快。但它使开发的多的速度更快。并提高你的代码的维护的
- 见1
采取MSDN文章性能注意事项(实体框架)
看看
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:
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:
Question
- I think EF gives very worst performance in first time execution Then why we use EF?
- Why EF second time execution was faster than first time execution?
- 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.
- See 1
Take a look on msdn article Performance Considerations (Entity Framework)
这篇关于性能分析ADO.NET和Entity Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!