Steven Sacks | BLITZ wrote:
It's the fastest because:

Pre-decrementation (--i) is faster than post-decrementation (i--)

I don't see why that would be. Both "var i=1;i--;" and "var i=1;--i;" compile down to the exact same 8 lines of p-code:

 _constantPool "i"
 _push "i" 1
 _var
 _push "i" "i"
 _getVariable
 _decrement
 _setVariable
 _end

So i don't see how one can be faster than the other. Maybe in other languages this is the case?


And substraction (- (-1) is faster than addition (+ 1).


I ran numerous tests using loops of 1 million on this but was unable to conclude one way or the other because roughly half the time one method was faster than the other. differences ranged from 1 millisecond to 175 milliseconds in both directions. My only take on my results is that they are at least negligably similar.

They are faster because it's less code that gets produced when you
compile.
http://flasm.sourceforge.net/#optimization


This isn't true. (i--) is one less line of p-code (and 9 less bytes of data) than (--i -(-1)) because it isn't doing the extra calculation of subtraction like I mentioned in my email. Take a look at the p-code:

var i:Number = 1;
while (i--) {
  trace(i);
}

        _constantPool "i"
        _push "i" 1
        _var
#4      _push "i"
        _getVariable
        _push "i" "i"
        _getVariable
        _decrement
        _setVariable
        _not
        _if true goto #16
        _push "i"
        _getVariable
        _trace
        _jump to #4
#16     _end

------------------------------
var i:Number = 1;
while (--i -(-1)) {
  trace(i);
}

        _constantPool "i"
        _push "i" 1
        _var
#4      _push "i" "i"
        _getVariable
        _decrement
        _storeRegister 0
        _setVariable
        _push register0 4294967295
        _subtract
        _not
        _if true goto #17
        _push "i"
        _getVariable
        _trace
        _jump to #4
#17     _end


For most cases, it's a matter of milliseconds or even less, which isn't
that big a deal, but I use it anyway because it's a good habit to have
and it makes my code logic work with that in mind always so when it does
make a difference in seconds, it's already there.


I agree about it not being a big deal, but I prefer the (i--) method over (--i -(-1)) since I find it easier to read.

-- James


James O'Reilly
www.jamesor.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

Reply via email to