对于VS的Foreach对阵列性能

对于VS的Foreach对阵列性能

本文介绍了对于VS的Foreach对阵列性能(在AS3 / Flex的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪一个是更快?为什么呢?

  VAR消息:数组= [......]

// 1  - 为
VAR LEN:INT = messages.length;
对于(VAR我:= 0; I< LEN;我++){
    VAR○:对象=消息[I]
    // ...
}

// 2  - 的foreach
每个(VAR○:对象的消息){
    // ...
}
 

解决方案

从那里我坐,经常循环适度快于每个循环中最小的情况下。此外,与AS2天,通过循环递减的方式通常提供了一个非常小的提升。

不过说真的,在这里任何细微的差别都会被你实际做的循环里面有什么要求相形见绌。你可以找到的操作,将工作更快或更慢在这两种情况下。真正的答案是,无论是那种循环可以有意义说是比其他快 - 因为它出现在你的应用程序,您必须配置您的code

样品code:

  VAR大小:数=千万;
VAR ARR:数组=​​ [];
对于(VAR我:= 0; I<大小;我++){ARR [我] =我; }
VAR时间:号码,邻:对象;

// 对于()
时间=的getTimer();
对于(i = 0; I<大小;我++){ARR [I] }
跟踪(测试+(的getTimer() - 时间)+毫秒);

//为()逆转
时间=的getTimer();
对于(I =尺寸-1; I> = 0;我 - ){ARR [I] }
跟踪(用于反向测试:+(的getTimer() - 时间)+毫秒);

//的for..in
时间=的getTimer();
每个(O在ARR){O; }
跟踪(对每个测试:+(的getTimer() - 时间)+毫秒);
 

结果:

 测试:124ms
为扭转测试:110毫秒
对于每个测试:261ms
 

编辑:为了提高比较,我改变了内循环,使他们做什么,但进入收藏价值

编辑2:答案oshyshko的评论:

  1. 在编译器可以跳过访问我的内部循环,但事实并非如此。环将退出两个或三个倍的速度,如果它是
  2. 在结果中您发布,因为该版本的样品code发生变化,循环现在有一个隐式类型转换。我离开分配我的循环,以避免这种情况。当然,人们可以说,这是可以有一个额外的倒在循环,因为真正的code的需要也无妨,但对我来说,这只是另一种方式说没有统一的答案;而循环更快要看你做什么,你的循环内。这就是答案我给你。 ;)

Which one is faster? Why?

var messages:Array = [.....]

// 1 - for
var len:int = messages.length;
for (var i:int = 0; i < len; i++) {
    var o:Object = messages[i];
    // ...
}

// 2 - foreach
for each (var o:Object in messages) {
    // ...
}
解决方案

From where I'm sitting, regular for loops are moderately faster than for each loops in the minimal case. Also, as with AS2 days, decrementing your way through a for loop generally provides a very minor improvement.

But really, any slight difference here will be dwarfed by the requirements of what you actually do inside the loop. You can find operations that will work faster or slower in either case. The real answer is that neither kind of loop can be meaningfully said to be faster than the other - you must profile your code as it appears in your application.

Sample code:

var size:Number = 10000000;
var arr:Array = [];
for (var i:int=0; i<size; i++) { arr[i] = i; }
var time:Number, o:Object;

// for()
time = getTimer();
for (i=0; i<size; i++) { arr[i]; }
trace("for test: "+(getTimer()-time)+"ms");

// for() reversed
time = getTimer();
for (i=size-1; i>=0; i--) { arr[i]; }
trace("for reversed test: "+(getTimer()-time)+"ms");

// for..in
time = getTimer();
for each(o in arr) { o; }
trace("for each test: "+(getTimer()-time)+"ms");

Results:

for test: 124ms
for reversed test: 110ms
for each test: 261ms

Edit: To improve the comparison, I changed the inner loops so they do nothing but access the collection value.

Edit 2: Answers to oshyshko's comment:

  1. The compiler could skip the accesses in my internal loops, but it doesn't. The loops would exit two or three times faster if it was.
  2. The results change in the sample code you posted because in that version, the for loop now has an implicit type conversion. I left assignments out of my loops to avoid that.Of course one could argue that it's okay to have an extra cast in the for loop because "real code" would need it anyway, but to me that's just another way of saying "there's no general answer; which loop is faster depends on what you do inside your loop". Which is the answer I'm giving you. ;)

这篇关于对于VS的Foreach对阵列性能(在AS3 / Flex的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:20