post increment or pre increment?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I learned in C++, that it is better to use pre increment rather than
post increment. I'm wondering which one is better to use in perl, for
example, in a for loop.

for(my $i = 0; $i <10; ++ $i)


for(my $i = 0; $i <10; $i ++)

Which one is better?


Re: post increment or pre increment?

Quoted text here. Click to load it
I think the same rules apply. I never use pre increment
in general.

But you would never do something like a function lvalue in C++:
if (func() == 5)...
better this
if (5 == func())


Re: post increment or pre increment? writes:

Quoted text here. Click to load it

These examples has nothing to do with lvalue functions and are
equivalent as long as we're not dealing with overloading of the
equality operator.

Lvalue functions is all about assignments and only assignments.


Re: post increment or pre increment?

Quoted text here. Click to load it

It depends on what you want to do. In that expression, do you need the
old value before the increment or the new value after the increment?

Quoted text here. Click to load it

Neither, nor. The best is a very simple and much more readable

    for my $i (0..9)


Re: post increment or pre increment?

Quoted text here. Click to load it

In the example he gave, he needs neither the old nor the new value. He
just needs the side effect. This is very common in C and C++, and the
only case where you pre- and postincrement are semantically equivalent,
so you can choose one or the other.

Pre-increment is semantically simpler. It increments the variable and
returns the new value. Post-increment needs to save the old value before
incrementing the variable, so that it can return the old value.

For simple variables this doesn't make any difference in C++, since the
compiler can just optimize the copy away. But for objects, it's not so
simple (the copy itself may cause some side-effect) so the usual advice
is to use pre-increment if you don't need the return value.

Perl has a very weak optimizer (because it needs to be fast), so
pre-increment might be faster.

Quoted text here. Click to load it

Why don't you just try it?

Quoted text here. Click to load it

use warnings;
use strict;
use Benchmark qw(cmpthese);

use constant N => 10;

cmpthese (-5,
            'pre'   => sub { for(my $i = 0; $i <N; ++ $i) {} },
            'post'  => sub { for(my $i = 0; $i <N; ++ $i) {} },
            'range' => sub { for my $i (0 .. N-1) {} },

Oops, the range is consistently the slowest:

          Rate range  post   pre
range 667769/s    --   -7%   -7%
post  714756/s    7%    --   -1%
pre   721696/s    8%    1%    --

          Rate range  post   pre
range 634634/s    --   -8%   -9%
post  691932/s    9%    --   -0%
pre   694498/s    9%    0%    --

          Rate range  post   pre
range 636271/s    --  -14%  -19%
post  739517/s   16%    --   -6%
pre   786373/s   24%    6%    --

but let's try N => 100:

          Rate   pre  post range
pre   103353/s    --   -0%  -27%
post  103859/s    0%    --  -27%
range 141336/s   37%   36%    --

          Rate  post   pre range
post   91995/s    --   -8%  -34%
pre   100124/s    9%    --  -28%
range 140021/s   52%   40%    --

          Rate  post   pre range
post   96837/s    --   -7%  -31%
pre   103855/s    7%    --  -26%
range 140913/s   46%   36%    --

Now, using a range is the fastest version, and that advantage increases for
larger loops:

N => 1000:

         Rate   pre  post range
pre   10094/s    --   -7%  -37%
post  10798/s    7%    --  -32%
range 15982/s   58%   48%    --

         Rate  post   pre range
post  10139/s    --   -2%  -35%
pre   10370/s    2%    --  -34%
range 15678/s   55%   51%    --

         Rate   pre  post range
pre   10647/s    --   -2%  -34%
post  10900/s    2%    --  -33%
range 16209/s   52%   49%    --

N => 10_000:

        Rate   pre  post range
pre    990/s    --  -10%  -38%
post  1099/s   11%    --  -31%
range 1597/s   61%   45%    --

        Rate  post   pre range
post   983/s    --   -5%  -39%
pre   1037/s    5%    --  -36%
range 1621/s   65%   56%    --

        Rate  post   pre range
post  1013/s    --   -3%  -38%
pre   1047/s    3%    --  -36%
range 1627/s   61%   55%    --

The difference between pre and post is generally small and sometimes pre
is faster and sometimes post is faster. So the difference between them
is probably less than the precision of the measurement.


PS: On my system, for this simple loop, all three variants are the same
speed for N in (14 .. 20). For lower values, range is consistently
slower than pre or post, for larger values, it is consistently faster.
If you have a different version of perl, a different processor, or a
more complex loop, results may be different.

PPS: N-1 is optimized by the compiler, so that doesn't make a

Re: post increment or pre increment?

Hi Peter,

Is this a copy paste error?

You seem to test twice the pre-increment:

Quoted text here. Click to load it

I changed the script to make pre and psot different and to use N=100
instead of N=10.

Range is now sometimes faster then pre/post.
Apart from this still quite random:

$ perl
     Rate   pre range  post
pre   69139/s    --  -14%  -18%
range 80766/s   17%    --   -4%
post  83995/s   21%    4%    --
$ perl
         Rate  post   pre range
post  59401/s    --   -5%  -33%
pre   62784/s    6%    --  -29%
range 88636/s   49%   41%    --
$ perl
         Rate   pre  post range
pre   60821/s    --   -1%  -27%
post  61321/s    1%    --  -26%
range 83067/s   37%   35%    --
$ perl
         Rate   pre  post range
pre   58606/s    --   -2%  -33%
post  60009/s    2%    --  -31%
range 87417/s   49%   46%    --


Peter J. Holzer wrote:
Quoted text here. Click to load it

Re: post increment or pre increment?

Peng Yu wrote:

Quoted text here. Click to load it

Which is better, depends on your needs.  Do you want to pre or post
increment in your loop?  You don't have to pre/post increment, when you
can use other functions to loop x number of times either.
Tim Greer, CEO/Founder/CTO,, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Re: post increment or pre increment?

Peng Yu wrote:
Quoted text here. Click to load it

Oh, really?  If the idea is to increment a variable, and you are not
going to immediately use the variable's old or new value, then it
makes no difference at all.


Site Timeline