.NET 迭代数据表中行的最快方法?

发布于 2024-07-07 17:09:10 字数 281 浏览 9 评论 0原文

从数据表中读取/比较行信息时,哪一个通常最快?

'assume dt as datatable'

'method 1'
dim i as int32
for i = 0 to dt.rows.count - 1
   ....
next

'method 2'
dim row as datarow
for each row in dt.rows
    ....
next

如果存在差异,在什么情况下使用其中一种比另一种更划算?

预先感谢您的任何指导!

Which is generally fastest when reading/comparing row info from a DataTable?

'assume dt as datatable'

'method 1'
dim i as int32
for i = 0 to dt.rows.count - 1
   ....
next

'method 2'
dim row as datarow
for each row in dt.rows
    ....
next

And if there's a difference, in what circumstances does it pay to use one over the other?

Thanks in advance for any guidance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

假装不在乎 2024-07-14 17:09:10

编译器将 For Each 扩展为一个短 while 循环。

for each row in dt.rows
// expands to:
IEnumerator e = dt.rows.GetEnumerator()
while e.MoveNext()
    row = e.Current

所以你需要支付少量的管理费用。 但为了清楚起见,如果您只处理一行并且不修改数据集,我仍然会坚持使用 For Each。

The compiler expands For Each to a short while loop.

for each row in dt.rows
// expands to:
IEnumerator e = dt.rows.GetEnumerator()
while e.MoveNext()
    row = e.Current

So you pay a small amount of overhead. But for clarities sake, I'd still stick with For Each if you're only working on one row, and you aren't modifying the data set.

两个我 2024-07-14 17:09:10

实际上没有区别。 尽管从技术上讲,您在 foreach 方法中为通过 IEnumerable 接口付出了很小的代价。

In reality no difference. Although you technically pay a small price in the foreach method, for going through the IEnumerable interface.

不回头走下去 2024-07-14 17:09:10

第二个将受到轻微处罚。 然而,就具体情况而言,为了代码的清晰性,我个人总是会使用方法 2。 但是,如果我需要执行某些操作(例如在分析当前行时访问下一行/上一行),我会使用方法 1。

The second would have a slight penalty. However as for circumstances, I persoanlly would always use method 2 for clarity of code. However, I would use method 1 if I ever need to do something such as accessing a next/previous row while analyzing the current row.

相思碎 2024-07-14 17:09:10

嗯,有一个区别,因为在 foreach 循环中调用的 GetEnumerator 和 MoveNext 是虚拟的(调用需要通过指针),因此不能内联。 实际上这个开销很小,除非您执行很多循环。

在某些情况下,编译器会将 foreach 替换为 for 循环(我相信在迭代数组时)。

为了清晰起见,我个人更喜欢在 ASP.NET MVC 代码中使用 foreach,正如许多人在这里所说的那样,但也经常使用 for 循环。
Joe Duffy 最近发表了一篇关于枚举成本的有趣文章
http://joeduffyblog.com/2008/09 /21/网络中枚举的成本/

Well, there is a difference, since GetEnumerator and MoveNext that get called in foreach-loop are virtual (calling requires going through a pointer) and thus can't be inlined. This overhead in reality is small, unless you do a lot of loops.

In some cases though the compiler will replace foreach with a for-loop (I believe when iterating over arrays).

I personally prefer foreach in my ASP.NET MVC code for clarity, as many have said here, but often use the for-loop too.
Joe Duffy recently posted an interesting article about the cost of enumerating
http://joeduffyblog.com/2008/09/21/the-cost-of-enumerating-in-net/

昨迟人 2024-07-14 17:09:10

@gdean232 是对的 - 几乎没有区别。 如果性能是一个问题,那么使用 SqlDataReader 会明显更快。

@gdean232 is right - almost no difference at all. If performance is an issue, using a a SqlDataReader instead is noticeable faster.

能怎样 2024-07-14 17:09:10

foreach 实现实际上比标准实现稍快一些,因为每个索引数组访问都需要进行边界检查。 然而,由于习语:

for(int i =0; i < myArray.Count; ++i)
{
    // do something with myArray[i];
}

很常见,编译器会将其视为特殊情况并对其进行优化,因此它变得更快。 但是,任何与该格式的小偏差(例如 int len = MyArray.Count; for(int i =0; i< len; ++i))都不会被识别,并且将使用较慢的代码。

The foreach implementation is actually slightly faster than the standard for implementation, because each index array access needs to be bounds checked. However, since the idiom:

for(int i =0; i < myArray.Count; ++i)
{
    // do something with myArray[i];
}

is common, the compiler looks for it as a special case and optimizes it, so it becomes faster. However, any small deviation from that format (such as int len = MyArray.Count; for(int i =0; i< len; ++i)) won't be recognized, and will use the slower code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文