Octavian Rasnita wrote:
> Hi,

Hello,

> Can anyone explain why:
> 
> $i++;
> 
> is faster than:
> 
> $i = 1;
> 
> and even more faster than:
> 
> $i += 1;

You have to understand how the CPU and Assembly Language works to truely
understand why one instruction is faster than another.  For example, assume a
CPU with the registers A and B:

;       $i++
        LOAD    A from $i
        INCR    A
        STORE   A to $i

;       $i += 1
        LOAD    A from $i
        LOAD    B from #memory_location_of_constant_1
        ADD     B to A
        STORE   A to $i

The CPU has to do more work to add two values then to just increment one.

And as to why ++$i is faster than $i++ ... well that it a bit beyond a Perl
beginners list.  :-)   (And besides, Perl optimizes $i++ to ++$i if the side
effects would not change the result.)


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


  • Re: $i++ Chris Devers
    • Re: $i++ John W. Krahn

Reply via email to