[英]ACEDAO performance in C# much slower than VB.net
Within a C# project, I need to fill an Access 2010 database with a lot of data, where performance is an issue. 在C#项目中,我需要使用大量数据填充Access 2010数据库,其中性能是个问题。 I did some testing using various data access techniques, and found out that using old fashioned DAO gives me the best performance (using a code example I found here on Stackoverflow).
我使用各种数据访问技术进行了一些测试,发现使用老式的DAO给了我最好的性能(使用我在Stackoverflow上找到的代码示例)。 The code below takes about 2.5 seconds to fill a 6-column table with 100,000 records.
下面的代码大约需要2.5秒来填充一个包含100,000条记录的6列表。
Dao.DBEngine eng = new Dao.DBEngine();
Dao.Database db = eng.OpenDatabase(@"d:\temp\speedtest.accdb");
eng.BeginTrans();
Dao.Recordset rs = db.OpenRecordset("tblTest");
Dao.Field[] myFields = new Dao.Field[6];
for (int k = 0; k <= 5; k++) {
myFields[k] = rs.Fields[k];
}
for (int i = 1; i <= 100000; i++) {
rs.AddNew();
myFields[0].Value = i;
myFields[1].Value = i;
myFields[2].Value = i;
myFields[3].Value = i.ToString();
myFields[4].Value = i.ToString();
myFields[5].Value = i.ToString();
rs.Update();
}
eng.CommitTrans();
Out of curiosity, I rewrote this piece of code, line by line, in VB.net. 出于好奇,我在VB.net中逐行重写了这段代码。
Dim eng As New Dao.DBEngine()
Dim db As Dao.Database = eng.OpenDatabase("d:\temp\speedtest.accdb")
eng.BeginTrans()
Dim rs As Dao.Recordset = db.OpenRecordset("tblTest")
Dim myFields() As Dao.Field = New Dao.Field(5) {}
For k As Integer = 0 To 5
myFields(k) = rs.Fields(k)
Next
Dim startTime As DateTime = DateTime.Now
For i As Integer = 1 To 100000
rs.AddNew()
myFields(0).Value = i
myFields(1).Value = i
myFields(2).Value = i
myFields(3).Value = i.ToString()
myFields(4).Value = i.ToString()
myFields(5).Value = i.ToString()
rs.Update()
Next
eng.CommitTrans()
The VB.net version runs in about 0.26 seconds or roughly 10 times faster than the C# version. VB.net版本运行大约0.26秒或大约比C#版本快10倍。 I find this difference staggering, and I can not explain this.
我发现这种差异令人咋舌,我无法解释这一点。 When I look at the IL code with Reflector, I can see no noticeable differences.
当我用Reflector查看IL代码时,我看不出明显的差异。 The projects are equal (both console applications), with the code above in the main method.
这些项目是相同的(两个控制台应用程序),上面的代码在main方法中。 Anyone any ideas how to get the C# version on the same performance?
任何想法如何获得相同性能的C#版本?
First of all, thanks to everyone who provided me with helpful hints and advice. 首先,感谢所有向我提供有用提示和建议的人。 I found the reason why the C# program was performing so badly, compared to it's VB counterpart.
我找到了C#程序表现如此糟糕的原因,与它的VB对应物相比。
If the C# code had been written as part of the 'normal' development solution, I would never have had this problem. 如果C#代码是作为“正常”开发解决方案的一部分编写的,我就不会遇到这个问题。 But unfortunately, when you start a C# console project, the main method does not get the [STAThread] attribute.
但不幸的是,当你启动一个C#控制台项目时,main方法没有获得[STAThread]属性。 I haven't figured out yet why a console program is lacking this attribute by default, but when added to my example program the C# and VB code perform exactly the same (give or take a few milliseconds).
我还没弄清楚为什么控制台程序默认缺少这个属性,但是当我添加到我的示例程序时,C#和VB代码执行完全相同(给出或花费几毫秒)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.