C# 性能分析 ADO.NET 和实体框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15107992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Performance analyze ADO.NET and Entity Framework
提问by Niventh
Which one gives better performance? ADO.NET or Entity Framework.
哪个性能更好?ADO.NET 或实体框架。
These are the two method I want to analyze.
这是我要分析的两种方法。
ADO.NET Test Method
ADO.NET 测试方法
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:
当我运行上述方法超过 100 次时。平均执行时间如图所示:
ADO.NET took only 2 milliseconds whether Entity Framework took more than 4 milliseconds.
ADO.NET 只用了 2 毫秒,无论实体框架用了超过 4 毫秒。
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:
当我在单次运行中一次又一次地运行这个方法时。ADO.NET 和 EF 之间的平均执行时间也不多:
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?
- 我认为 EF 在第一次执行时的性能非常差 那为什么我们使用 EF?
- 为什么 EF 第二次执行比第一次执行快?
采纳答案by Sergey Berezovskiy
- 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 muchfaster. And improves maintainabilityof your code.
- See 1
- EF 第一次将元数据加载到内存中,这需要一些时间。它从 edmx 文件或源代码(如果您首先使用代码)构建模型的内存表示。实际上 EF 是在 ADO.NET 的顶部构建的,所以它不能更快。但它使发展多快。并提高代码的可维护性。
- 见 1
Take a look on msdn article Performance Considerations (Entity Framework)
查看 msdn 文章性能注意事项(实体框架)
回答by Jens H
- 1) EF makes a lot of things more comfortablewhen working with databases. There is a lot going on under the hood that you otherwise would have to code manually.
- 1) EF在使用数据库时让很多事情变得更加舒适。有很多事情发生在引擎盖下,否则您将不得不手动编码。
For example, one of my first bigger projects was dealing a lot with data and I implemented the access layer with ADO.NET. This made up for something between a quarter or even a third of the whole project.
例如,我的第一个更大的项目之一是处理大量数据,我使用 ADO.NET 实现了访问层。这弥补了整个项目的四分之一甚至三分之一。
With my experience of the EF today I could get rid of nearly all of that! I just makes lots of the complicated code I wrote by hand completely unneccessary. We are talking about thousands of lines here.
以我今天对 EF 的经验,我几乎可以摆脱所有这些!我只是让很多我手工编写的复杂代码完全没有必要。我们在这里谈论的是数千行。
- 2) Two main reasons here. First, EF is built on top of using ADO.NET. This means that everthing EF does, add more overhead to whatever ADO would do. Second (very) simply put, the JIT compiler compiles the code for the first time just when it is executed. This includes memory allocation and all sorts of initializations.
- 2)这里有两个主要原因。首先,EF 建立在使用 ADO.NET 之上。这意味着 EF 所做的一切都会为 ADO 所做的任何事情增加更多的开销。其次(非常)简单地说,JIT 编译器在代码执行时第一次编译它。这包括内存分配和各种初始化。
This means that code you run multiple times runs much faster from the second time on. If you execute your EF queries only once, on the other hand, you will have no gains from those initializations.
这意味着您多次运行的代码从第二次开始运行得更快。另一方面,如果您只执行一次 EF 查询,您将不会从这些初始化中获得任何收益。
In a real world application you might try to do some optimizations like using Compiled Queries. Performance wise this will help you a lot because now your queries do not need to be prepared and compiled each time you run them but only once.
在现实世界的应用程序中,您可能会尝试进行一些优化,例如使用Compiled Queries。性能方面,这将对您有很大帮助,因为现在您的查询不需要在每次运行时准备和编译,而只需一次。
回答by Bruno Guardia
While working at Microsoft, I wrote a blog post comparing the performance of both. Seems it is now in the process of being migrated, so you may need to go to the Internet archive to find it...
在 Microsoft 工作期间,我写了一篇博客文章比较了两者的性能。好像现在正在迁移中,所以你可能需要去互联网档案馆找到它......
We had focused a lot on making sure the performance cost of using EF was not terrible, not perfect in V1 but quite usable.
我们非常注重确保使用 EF 的性能成本并不可怕,在 V1 中并不完美但非常可用。
While almost 10 years later the EF team has done a good work improving performance, particularly reducing the bad case scenarios, by design the Entity Framework sits over ADO.Net. So if your primary criteria is raw performance, you should go for ADO.Net, with hand-optimized SQL.
虽然近 10 年后 EF 团队在提高性能方面做了很好的工作,特别是减少了糟糕的情况,但实体框架的设计却是基于 ADO.Net。因此,如果您的主要标准是原始性能,那么您应该选择带有手动优化 SQL 的 ADO.Net。
That being said, many otherwise good developers, don't craft the best SQL; Entity Framework isolates them from writing the queries, and uses good practices to produce reasonably good queries.
话虽如此,许多其他优秀的开发人员并没有制作出最好的 SQL;实体框架将它们与编写查询隔离开来,并使用良好的实践来生成合理的良好查询。
The major advantage of the Entity Framework is providing a higher level of abstraction to work with data, isolating the app developer from the underlying data model. So you would use the EF to be more productive, writing less data access code; you still can fine tune specific queries or data operations, without losing the abstraction that makes programming easier on the non-performance-critical code, which is the largest part of any business aplication, for example.
实体框架的主要优点是提供更高级别的抽象来处理数据,将应用程序开发人员与底层数据模型隔离开来。因此,您将使用 EF 提高工作效率,编写更少的数据访问代码;您仍然可以微调特定的查询或数据操作,而不会丢失使非性能关键代码编程更容易的抽象,例如,这是任何业务应用程序的最大部分。
回答by Anuj
I think EF gives very worst performance in first time execution Then why we use EF?
我认为 EF 在第一次执行时的性能非常差 那为什么我们使用 EF?
- Auto generated codes for data access layer
- Reduces development time and cost
- Allows LINQ queries as well.
- 数据访问层自动生成代码
- 减少开发时间和成本
- 也允许 LINQ 查询。
Why EF second time execution was faster than first time execution?
为什么 EF 第二次执行比第一次执行快?
Yes. One of the most important features of EF is caching.
是的。EF 最重要的特性之一是缓存。