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?
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.
Why don't you just try it?
Neither, nor. The best is a very simple and much more readable
for my $i (0..9)
#!/usr/bin/perl
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) {} },
});
__END__
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.
hp
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
difference.