Created
May 31, 2010 18:20
-
-
Save banthar/420110 to your computer and use it in GitHub Desktop.
flash loop benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Sprite; | |
import flash.utils.getTimer; | |
public class Test extends Sprite | |
{ | |
private static var a:Array=new Array(); | |
private static var v:Vector.<int>=new Vector.<int>(1000000); | |
private static function testFor():void | |
{ | |
var sum:int=0; | |
for(var i:int=0;i<a.length;i++) | |
sum+=a[i]; | |
if(sum!=1783293664) | |
throw "fail"; | |
} | |
private static function testForLocalLength():void | |
{ | |
var len:int=a.length; | |
var sum:int=0; | |
for(var i:int=0;i<len;i++) | |
sum+=a[i]; | |
if(sum!=1783293664) | |
throw "fail"; | |
} | |
private static function testReversed():void | |
{ | |
var sum:int=0; | |
for(var i:int=a.length-1;i>=0;i--) | |
sum+=a[i]; | |
if(sum!=1783293664) | |
throw "fail"; | |
} | |
private static function testForIn():void | |
{ | |
var sum:int=0; | |
for(var i:String in a) | |
sum+=a[i]; | |
if(sum!=1783293664) | |
throw "fail"; | |
} | |
private static function testForEachIn():void | |
{ | |
var sum:int=0; | |
for each(var i:int in a) | |
sum+=i; | |
if(sum!=1783293664) | |
throw "fail"; | |
} | |
private static function testVector():void | |
{ | |
var sum:int=0; | |
for(var i:int=0;i<v.length;i++) | |
sum+=v[i]; | |
if(sum!=1783293664) | |
throw "fail"; | |
} | |
private static function testVectorReversed():void | |
{ | |
var sum:int=0; | |
for(var i:int=v.length-1;i>=0;i--) | |
sum+=v[i]; | |
if(sum!=1783293664) | |
throw "fail"; | |
} | |
private static function testVectorEach():void | |
{ | |
var sum:int=0; | |
for each(var i:int in v) | |
sum+=i; | |
if(sum!=1783293664) | |
throw "fail"; | |
} | |
private static function benchmark(f:Function,text:String):void | |
{ | |
var start:int=getTimer(); | |
f(); | |
trace(text,": ",getTimer()-start,"ms"); | |
} | |
public function Test() | |
{ | |
for(var i:int=0;i<1000000;i++) | |
a.push(i); | |
for(i=0;i<1000000;i++) | |
v.push(i); | |
benchmark(testFor,"for(var i:int=0;i<a.length;i++)"); | |
benchmark(testReversed,"for(var i:int=a.length-1;i>=0;i--)"); | |
benchmark(testForLocalLength,"for(var i:int=0;i<len;i++)"); | |
benchmark(testForIn,"for(var i:String in a)"); | |
benchmark(testForEachIn,"for each(var i:int in a)"); | |
benchmark(testVector,"for(var i:int=0;i<v.length;i++) using Vector"); | |
benchmark(testVectorReversed,"for(var i:int=v.length-1;i>=0;i--) using Vector"); | |
benchmark(testVectorEach,"for each(var i:int in v) using Vector"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
well guys, you're fooling yourself here ... a lot.
using for example for(var i:int=0; i < v.length; i++) simply is slower than for(var i:int=v.length-1;i>=0;i--) because there is a request to the .length property of the vector every loop. if you store v.length in a var before the for-loop, you will get the same result as for the "--" version.