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):
1 2 3 4 5 6 7 8 9 10 |
#!/usr/bin/perl 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:
1 2 |
pre 135196/s -- -5% fix 141940/s 5% -- |
We see that we always should choose the for loop with a fixed range its possible.