在.NET中,其中循环运行速度更快,“对”或“的foreach”? [英] In .NET, which loop runs faster, 'for' or 'foreach'?

查看:412
本文介绍了在.NET中,其中循环运行速度更快,“对”或“的foreach”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#/ VB.NET / .NET,其中循环运行速度更快,的foreach

In C#/VB.NET/.NET, which loop runs faster, for or foreach?

自从我读了一个循环的作品比一个的foreach 循环中的很久以前我以为这站适用于所有的集合,泛型集合,所有的数组,等等

Ever since I read that a for loop works faster than a foreach loop a long time ago I assumed it stood true for all collections, generic collections, all arrays, etc.

我搜罗谷歌和发现了一些文章,但他们大多是不确定的(阅读文章评论)和开放性。

I scoured Google and found a few articles, but most of them are inconclusive (read comments on the articles) and open ended.

什么是理想的是有列出的每个场景,并为同一个最好的解决方案。

What would be ideal is to have each scenario listed and the best solution for the same.

例如(它应该如何只是一个例子):

For example (just an example of how it should be):

  1. 进行迭代的1000+数组 字符串 - 好过的foreach
  2. 用于遍历的IList (非通用)字符串 - 的foreach 更好 比
  1. for iterating an array of 1000+ strings - for is better than foreach
  2. for iterating over IList (non generic) strings - foreach is better than for

在网上找到了相同的一些参考:

A few references found on the web for the same:

  1. 原始古老的大文章灵光Schanzer
  2. $ C $的CProject FOREACH比。 FOR
  3. 博客 - 要的foreach 还是不要的foreach ,这是个问题
  4. ASP.NET论坛 - NET 1.1 C# VS 的foreach
  1. Original grand old article by Emmanuel Schanzer
  2. CodeProject FOREACH Vs. FOR
  3. Blog - To foreach or not to foreach, that is the question
  4. ASP.NET forum - NET 1.1 C# for vs foreach

除了它的可读性方面,我在事实和数字很感兴趣。在一些应用中的性能优化的最后一英里挤做的事。

Apart from the readability aspect of it, I am really interested in facts and figures. There are applications where the last mile of performance optimization squeezed do matter.

推荐答案

的foreach 环显示比更具体的意向循环

foreach loops demonstrate more specific intent than for loops.

使用的foreach 循环演示了使用code,你打算做些什么来集合中的每个成员,不论其在收藏位置中的任何一个。它也表明你没有修改原始集合(如果你试图抛出异常)。

Using a foreach loop demonstrates to anyone using your code that you are planning to do something to each member of a collection irrespective of its place in the collection. It also shows you aren't modifying the original collection (and throws an exception if you try to).

的foreach 的另一个好处是,它适用于任何的IEnumerable ,其中以才有意义的IList ,其中每个元素实际上有一个索引。

The other advantage of foreach is that it works on any IEnumerable, where as for only makes sense for IList, where each element actually has an index.

然而,如果你需要使用元素的索引,那么你当然应该被允许使用循环。但是,如果你不需要使用索引,有一个是刚刚塞满您的code。

However, if you need to use the index of an element, then of course you should be allowed to use a for loop. But if you don't need to use an index, having one is just cluttering your code.

有没有显著的性能影响,因为据我所知。在未来的某个阶段,可能会更容易使用,以适应code 的foreach 来在多个内核上运行,但这不是担心现在。

There are no significant performance implications as far as I'm aware. At some stage in the future it might be easier to adapt code using foreach to run on multiple cores, but that's not something to worry about right now.

这篇关于在.NET中,其中循环运行速度更快,“对”或“的foreach”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆