Then I tried the ol' benchmark and found that the two are more or less equivelent, which is what I expected. If anything the reverse method is slightly faster.C:\>perl -MO=Deparse -we "$a = 'doit'; print \"hi\n\" if $a eq 'doit'" $a = 'doit'; print "hi\n" if $a eq 'doit'; -e syntax OK C:\>perl -MO=Deparse -we "$a = 'doit'; if ($a eq 'doit'){print \"hi\n\ +"}" $a = 'doit'; if ($a eq 'doit') { print "hi\n"; } -e syntax OK
#!perl -w use strict; use Benchmark; sub Time { use vars qw( $clause ); $clause = shift; timethese( -5, { normal => sub { my $i = 0; if( $clause ) { ++$i } }, reverse => sub { my $i = 0; ++$i if $clause }, } ); } Time(1); Time(0); __END__ Output: Benchmark: running normal, reverse, each for at least 5 CPU seconds... normal: 6 wallclock secs ( 5.00 usr + 0.00 sys = 5.00 CPU) @ 97 +5640.40/s (n=4878202) reverse: 5 wallclock secs ( 5.21 usr + 0.00 sys = 5.21 CPU) @ 10 +03193.28/s (n=5226637) Benchmark: running normal, reverse, each for at least 5 CPU seconds... normal: 5 wallclock secs ( 5.27 usr + 0.00 sys = 5.27 CPU) @ 15 +65443.64/s (n=8249888) reverse: 4 wallclock secs ( 5.11 usr + 0.00 sys = 5.11 CPU) @ 15 +85628.38/s (n=8102561)
In reply to Re: Loops and speed
by Adam
in thread Loops and speed
by damian1301
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |