Perl increment Benchmark

This is a Perl Benchmark about post-increment,pre-increment and a normal increment:

#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw(:all) ;
 

cmpthese(-1, {
'pre'   => sub {for(my $a=0;$a<=10;++$a){print $a;} },
'post'  => sub {for(my $a=0;$a<=10;$a++){print $a;} },
'dev'   => sub {for(my $a=0;$a<=10;$a+=1){print $a;} },
});

This is our result:

dev  124842/s   --  -9% -16%  
post 136533/s   9%   --  -8%  
pre  149123/s  19%   9%   --

pre increment is the fastest because it have not to increment the value and return the old value.But the fastest is the fixed range, take a look at for loop Benchmark.The default is the slowest because it have to handle other steps.

One thought on “Perl increment Benchmark”

Leave a Reply

Your email address will not be published.

nineteen − fourteen =