for loop Benchmark

This is a Perl Benchmark about the for loop with fix range and with a post increment that’s the fastest increment (Perl increment Benchmark):

use strict;
use warnings;
use Benchmark qw(:all) ;

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

Our result:

pre 135196/s  -- -5% 
fix 141940/s  5%  --

We see that we always should choose the for loop with a fixed range its possible.

Leave a Reply

Your email address will not be published.

5 + thirteen =