Hello Anonymous Monk,
Have you considered also the possibility of Switch? (Not Recommended)
#!usr/bin/perl use say; use strict; use warnings; # use Benchmark qw(:all) ; # WindowsOS use Benchmark::Forking qw( timethese cmpthese ); sub while_loop { my (@array) = @_; while (defined(my $i = shift @array)){ # This destroys @array, tho +ugh next if ($i % 2 || $i % 3 || $i % 4 || $i % 5); } } sub for_loop { my (@array) = @_; foreach my $i ( @array ) { # This does not destroy the @array next if ($i % 2 || $i % 3 || $i % 4 || $i % 5); } } my $results = timethese(100000000, { For => for_loop( 1 .. 100 ), While => while_loop( 1 .. 100 ) }, 'none'); cmpthese( $results ); __END__ $ perl test.pl (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) Rate For While For 434782609/s -- -83% While 2500000000/s 475% --
Update: Not a good idea as the monks pointed out. Not recommended it.
Update2: You could also change your for loop to a while loop it is always faster see code example above.
Hope this helps.
In reply to Re: if ... elsif ... else
by thanos1983
in thread if ... elsif ... else
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |