Hi,
I often asked myself if a[ a.length ] = xxx was faster or slower then
a.push( xxx ), I did some test at wake up, fresh with coffee.
So now I know the answer and I got a bit more about while and for, and
more obvious about using them with decremental or incremental counters.
from results, > means faster :
for > while ....hey yes...oO
increment > decrement
length > push
increment or certainly make any number operation at same time than
putting the value in variable is slower than separate those actions, like:
a[ a.length ] = i++;
slower than:
i++;
a[ a.length ] = i;
"while incremental" is faster than "for decremental".
This is a totaly useless information as if you can use the while
incremental instead of For decremental, then just use For incremental.
I heard that any loop was compiled to a while loop so I started coding
everything with a while, what is faster to write and more elegant.
Now I'll go back to those For loops, I promess I never stoped loving you
guys...
here some convincing results :
takeLengthMinus : 1695
takeLengthMinusOut : 1598
takeLengthPlus : 1580
takeLengthPlusOut : 1550
takePushMinus : 1860
takePushMinusOut : 1768
takePushPlus : 1756
takePushPlusOut : 1685
Don't compare results between separate paragraphe because they did not
run all together:
takeForWithLengthMinus : 1624
takeForWithLengthPlus : 1581
The For loop is directly with operation outside, so it has to be
compared to the
other outside operations:
takeLengthMinusOut : 1686
takeLengthPlusOut : 1610
takePushMinusOut : 1788
takePushPlusOut : 1666
takeForWithLengthMinus : 1626
takeForWithLengthPlus : 1563
I guess that's why I learned to use for( i = 0; i < n; i++ ) for my
first loops.
So if we want our code faster we have to make it longer and actually
more human readable, at the same time it means more computer readable as
it gets faster....hm, is the computer so close to human...?!
I [ mean my brain ] actually use a dicotomic way to find my current
client folder in the list of all my works.
cheers.
L
and here the codes :
function takeLengthMinus():String{
var i : int = 10000000;
var a: Array = new Array();
var t: Number = getTimer();
while( i-- ){
a[ a.length ] = i;
}
return "takeLengthMinus : " + ( getTimer() - t );
}
function takeLengthMinusOut():String{
var i : int = 10000000;
var a: Array = new Array();
var t: Number = getTimer();
while( i ){
a[ a.length ] = i;
i--;
}
return "takeLengthMinusOut : " + ( getTimer() - t );
}
function takeLengthPlus():String{
var i : int = 0;
var a: Array = new Array();
var t: Number = getTimer();
while( i < 10000000 ){
a[ a.length ] = i++;
}
return "takeLengthPlus : " + ( getTimer() - t );
}
function takeLengthPlusOut():String{
var i : int = 0;
var a: Array = new Array();
var t: Number = getTimer();
while( i < 10000000 ){
a[ a.length ] = i;
i++;
}
return "takeLengthPlusOut : " + ( getTimer() - t );
}
function takePushMinus():String{
var i : int = 10000000;
var a: Array = new Array();
var t: Number = getTimer();
while( i-- ){
a.push( i );
}
return "takePushMinus : " + ( getTimer() - t );
}
function takePushMinusOut():String{
var i : int = 10000000;
var a: Array = new Array();
var t: Number = getTimer();
while( i ){
i--;
a.push( i );
}
return "takePushMinusOut : " + ( getTimer() - t );
}
function takePushPlus():String{
var i : int = 0;
var a: Array = new Array();
var t: Number = getTimer();
while( i < 10000000 ){
a.push( i++ );
}
return "takePushPlus : " + ( getTimer() - t );
}
function takePushPlusOut():String{
var i : int = 0;
var a: Array = new Array();
var t: Number = getTimer();
while( i < 10000000 ){
a.push( i );
i++;
}
return "takePushPlusOut : " + ( getTimer() - t );
}
function takeForWithLengthMinus():String{
var i : int;
var a: Array = new Array();
var t: Number = getTimer();
for( i = 10000000; i > 0; i-- ){
a[ a.length ] = i;
}
return "takeForWithLengthMinus : " + ( getTimer() - t );
}
function takeForWithLengthPlus():String{
var i : int;
var a: Array = new Array();
var t: Number = getTimer();
for( i = 0; i < 10000000; i++ ){
a[ a.length ] = i;
}
return "takeForWithLengthPlus : " + ( getTimer() - t );
}
//trace( takeLengthMinus() );
trace( takeLengthMinusOut() );
//trace( takeLengthPlus() );
trace( takeLengthPlusOut() );
//trace( takePushMinus() );
trace( takePushMinusOut() );
//trace( takePushPlus() );
trace( takePushPlusOut() );
trace( takeForWithLengthMinus() );
trace( takeForWithLengthPlus() );
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders