对于数组来说哪个更快 - foreach或者(i ...)? [英] Which is faster for arrays - foreach or for ( i... ) ?

查看:69
本文介绍了对于数组来说哪个更快 - foreach或者(i ...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




给出以下声明:


object [] a =新对象[SOME_BIG_NUMBER];


哪个代码更快?


1.

foreach(对象o)a / b
DoSomethingWith (o);


2.

int iSize = a.Length;

int i;

foreach(i = 0; i< iSize; i ++)

DoSomethingWith(a [i]);


为什么?


问候,

DoB

Hi,

Given the following declaration:

object[] a = new object[SOME_BIG_NUMBER];

which code is faster?

1.
foreach ( object o in a )
DoSomethingWith(o);

2.
int iSize = a.Length;
int i;
foreach ( i = 0; i < iSize; i++ )
DoSomethingWith(a[i]);

And why?

Regards,
DoB

推荐答案

foreach(i = 0; i< iSize ; i ++)


对不起,这里有一个错字,当然;-)。应该是for。


DoB
foreach ( i = 0; i < iSize; i++ )

Sorry, there was a typo here, of course ;-). should be "for".

DoB


" DoB" < Do*@dob.com写信息

新闻:ex ************** @ TK2MSFTNGP06.phx.gbl ...
"DoB" <Do*@dob.comwrote in message
news:ex**************@TK2MSFTNGP06.phx.gbl...

给出以下声明:


object [] a =新对象[SOME_BIG_NUMBER];


哪个代码更快?


1.

foreach(对象o in a)

DoSomethingWith(o);


2.

int iSize = a.Length;

int i;

foreach(i = 0 ; i< iSize; i ++)

DoSomethingWith(a [i]);


为什么?
Given the following declaration:

object[] a = new object[SOME_BIG_NUMBER];

which code is faster?

1.
foreach ( object o in a )
DoSomethingWith(o);

2.
int iSize = a.Length;
int i;
foreach ( i = 0; i < iSize; i++ )
DoSomethingWith(a[i]);

And why?



一般来说,for i与数组一起使用比foreach快,

的原因是for i只需要递增并比较一个整数和

为每次迭代做一个索引操作,这是快速操作,

而foreach需要调用IEnumerator移动到下一个元素

然后检索当前元素。这两个方法调用,另外

到方法内部的任何内容,因此它有更大的开销。

In general, "for i" used with an array is faster than "foreach", the
reason being that "for i" only needs to increment and compare an integer and
do an indexing operation for every iteration, which are fast operations,
while "foreach" needs to call into IEnumerator to move to the next element
and then retrieve the current one. These are two method calls, in addition
to whatever is inside the methods, so it has a bigger overhead.


On 2月6日上午9:50,DoB < D ... @ dob.comwrote:
On Feb 6, 9:50 am, "DoB" <D...@dob.comwrote:

给出以下声明:


object [] a = new对象[SOME_BIG_NUMBER];


哪个代码更快?
Given the following declaration:

object[] a = new object[SOME_BIG_NUMBER];

which code is faster?



我不会专注于哪些代码更快,直到我证明了这一点

很重要 - 此时我''如果我做了
更改,有数字要比较。


我会专注于哪个更易读,更简单 - 在这种情况下

" foreach"在大多数情况下都是胜利。


在你知道自己遇到问题之前不要进行微观优化 - 如果你这样知道你知道的话有一个问题,应该包括硬数据,这将使b / b更容易测试变化。


Jon

I wouldn''t concentrate on which code is faster until I''ve proved it
matters - at which point I''d have numbers to compare if I made
changes.

I''d concentrate on which is more readable, and simpler - in which case
"foreach" wins in most situations.

Don''t micro-optimise until you know you''ve got a problem - and if you
know you''ve got a problem, that should include hard figures, which
will make it easy to test changes.

Jon


这篇关于对于数组来说哪个更快 - foreach或者(i ...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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