Optimisation

Simon Wistow simon at thegestalt.org
Mon Mar 2 21:30:24 GMT 2009


On Mon, Mar 02, 2009 at 09:11:29PM +0000, Andy Armstrong said:
> Benchmark it - but I bet there's not much difference - and the second 
> version will be faster in the non-empty case.

With 1,000,000 iterations

          Rate no_if    if
no_if 501961/s    --   -2%
if    514056/s    2%    --


With 10,000,000

          Rate no_if    if
no_if 504334/s    --   -2%
if    514263/s    2%    --



-- >8 --

use Benchmark qw(:all);

my $counter1 = 0;
my $counter2 = 0;
my $null = undef;
cmpthese(1000000, {
    'if' => sub {
       my @list = ($counter1++%2)? qw(a b c d e f g h i) : ();
       if (@list) { foreach (@list) { $null = $_ } }
    },
    'no_if' => sub {
       my @list = ($counter2++%2)? qw(a b c d e f g h i) : ();
       foreach (@list) { $null = $_ }
    }
});

-- >8 --


More information about the london.pm mailing list