Hello :)
Yes it's funny .. i forget the 0 is a boolean in a conditional
instruction... the problem with this loop is the negative values. but it's a
good notation.
After tests... in AS2 this method is more speed.
var ar:Array = new Array(500000) ;
var time ;
Key.addListener(this) ;
onKeyDown = function()
{
var result ;
var code:Number = Key.getCode() ;
var copy:Array = [].concat(ar)
var len:Number = copy.length ;
switch (code)
{
case Key.UP :
{
time = getTimer() ;
for( var i:Number = 0 ; i<len ; i++ )
{
result = copy[len] ;
}
time = getTimer() - time ;
trace( "for speed : " + time + " ms") ;
break ;
}
case Key.DOWN :
{
time = getTimer() ;
while( len-- )
{
result = copy[len] ;
}
time = getTimer() - time ;
trace( "while(len--) speed : " + time + " ms") ;
break ;
}
case Key.LEFT :
{
time = getTimer() ;
while( --len > -1 )
{
result = copy[len] ;
}
time = getTimer() - time ;
trace( "while(--len > -1) speed : " + time + " ms") ;
break ;
}
case Key.RIGHT :
{
time = getTimer() ;
while( --len -(-1) )
{
result = copy[len] ;
}
time = getTimer() - time ;
trace( "while(--len -(-1)) speed : " + time + " ms") ;
break ;
}
}
}
for speed : 329 ms
while(len--) speed : 305 ms
while(--len > -1) speed : 324 ms
while(--len -(-1)) speed : 418 ms
Thanks :)
EKA+ :)
2007/5/11, Steven Sacks <[EMAIL PROTECTED]>:
I think he was pointing out how the loop could be optimized even further.
Pre-decrementing is faster than post-decrementing.
However, his greater than comparison slows it down. The right way to do
it is:
while (--i -(-1))
{
}
since subtraction is faster than addition.
This is all academic, though. I used to use --i -(-1) but I gave it up
for the readability of while (i--).
This is my new favorite:
while (i--) i++;
;)
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com