Enumerator vs ForNext vs ForEach [英] Enumerator vs ForNext vs ForEach

查看:56
本文介绍了Enumerator vs ForNext vs ForEach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




哪个性能更好。


迭代枚举器

ForNext循环(使用索引器)

ForEach循环


Thanx

rawCoder

Hi,

Which is better in terms of performance.

Iterating over Enumerator
ForNext loop (using indexer)
ForEach loop

Thanx
rawCoder

推荐答案

我猜测正常的for循环只是增加一个变量。


我想你可以很容易地测试它,但不必在一个

新闻组。只需对大量数据进行一些测试。


" rawCoder" < RA ****** @ hotmail.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...
I''m guessing the regular for loop with just incrementing a variable.

I guess you can test this pretty easily though without having to ask in a
newsgroup. Just run some tests on large sets of data.

"rawCoder" <ra******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...


在性能方面哪个更好。

迭代枚举器
ForNext循环(使用索引器)
ForEach循环
Thanx
rawCoder
Hi,

Which is better in terms of performance.

Iterating over Enumerator
ForNext loop (using indexer)
ForEach loop

Thanx
rawCoder






我记得第二个是速度最快,但是有些人声称第一个是同等性能的
(而第三个实际上意味着第一个带有粘合的

代码在后台编译)。

我建议您使用所有这些

方法编写简单的性能测试并在此处发布结果。


此外,您可以参考''提高.NET应用程序的性能''

电子书(不是确切的标题,应该可以从模式和实践中获得

网站)。


-

此致,

Dmytro Lapshyn [视觉D eveloper - Visual C#MVP]

" rawCoder" < RA ****** @ hotmail.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...
Hi,

I remember the second was the fastest, but some people claim the 1st is
equally performant (and the 3rd actually implies the 1st with the gluing
code compiled for you in the background).
I''d suggest that you wrote a simple performance test using all these
approaches and post your results here.

Also, you can consult the ''Improving the performance of .NET applications''
e-book (not exact title, should be available from the Patterns and Practices
website).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"rawCoder" <ra******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...


在性能方面哪个更好。

迭代枚举器
ForNext循环(使用索引器)
ForEach循环
Thanx
rawCoder
Hi,

Which is better in terms of performance.

Iterating over Enumerator
ForNext loop (using indexer)
ForEach loop

Thanx
rawCoder






rawCoder,


它'很难说。 IEnumerable / IEnumerator的每个实现都将与b
不同。每个集合对象可以不同的方式处理枚举。


但是,我认为大多数实现只会在IEnumerator的实现中保留一个

内部计数器然后访问使用该内部计数器的

集合(与每个循环使用for

相同)。这假设该集合适合于这种类型的b
迭代(例如,树结构将完全不同)。


一般来说,它最好使用foreach,因为如果IEnumerator的

实现也实现了IDisposable,它也将处理

处理实现。

希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" rawCoder" < RA ****** @ hotmail.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...
rawCoder,

It''s hard to say. Each implementation of IEnumerable/IEnumerator will
be different. Each collection object can handle enumerations differently.

However, I would think that most implementations will just keep an
internal counter in the implementation of IEnumerator and then access the
collection using that internal counter (which is the same as using a for
each loop). This assumes that the collection is suited for this kind of
iteration (a tree structure would be completely different, for example).

Generally speaking though, it is better to use foreach, because if the
implementation of IEnumerator also implements IDisposable, it will handle
the disposing of the implementation as well.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"rawCoder" <ra******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...


在性能方面哪个更好。

迭代枚举器
ForNext循环(使用索引器)
ForEach循环
Thanx
rawCoder
Hi,

Which is better in terms of performance.

Iterating over Enumerator
ForNext loop (using indexer)
ForEach loop

Thanx
rawCoder



这篇关于Enumerator vs ForNext vs ForEach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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