#!/usr/local/bin/perl -w use strict; use Benchmark; my @foo = map {int rand 3} 1..100; open(OUT, ">bm.out") or die "$!\n"; # Note: bm.out will be a large file when this is done. :) timethese(-10, { mod => sub { for (@foo) { print OUT "$_" if $_; } print OUT "\n"; }, block => sub { for (@foo) { if ($_) { print OUT "$_"; } } print OUT "\n"; }, and => sub { for (@foo) { $_ and print OUT "$_"; } print OUT "\n"; }, } ); __END__ Benchmark: running and, block, mod, each for at least 10 CPU seconds... and: 3554.42/s block: 3530.86/s mod: 3511.90/s #### #!/usr/local/bin/perl -w use strict; use Benchmark; my $foo = 0; # $foo will be toggled between true and false my $x; timethese(-10, { mod => sub { $x = 1 if $foo ^= 1 }, block => sub { if ($foo ^= 1) { $x = 1 } }, and => sub { $foo ^= 1 and $x = 1 }, } ); __END__ Benchmark: running and, block, mod, each for at least 10 CPU seconds... and: 233020.67/s block: 230307.69/s mod: 234463.20/s Benchmark: running and, block, mod, each for at least 10 CPU seconds... and: 237002.40/s block: 236647.46/s mod: 226841.12/s