in reply to if ... elsif ... else

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.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: if ... elsif ... else
by Corion (Patriarch) on Jun 01, 2017 at 09:22 UTC

    Please do not recommend Switch. The module is a source filter and thus introduces subtle problems if the author of a script using Switch is not careful.

    A source filter is certainly not suitable for beginners in programming who are fighting errors of their own making, as it introduces more errors that have no obvious correlation to the source code.

Re^2: if ... elsif ... else (do not use Switch)
by hippo (Archbishop) on Jun 01, 2017 at 09:25 UTC

    Please do not recommend use of the Switch module. It has been long deprecated and has been removed from modern perls. See Re: Switch Module on Perl 5.8.0 for some details of the subject and more links.