http://www.perlmonks.org?node_id=791905

sir_com has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

going through some site or in some post in perlmonk site, I saw that there is a difference in the for loop syntax. They say that for $i (1..10) is much efficient than for($i=0;$i<10;$i++). For example the below code for $i (1..10) { print "HI\n"; } is efficient than for($i=0;$i<10;$i++) { print "HI\n"; }. I don't understand where does the efficiency part coming here, cause in both the cases the "print" statement going to execute for 10 or n times. Then how does the first "for" syntax is efficient? Can somone please explain?

thanks, sir_com

Replies are listed 'Best First'.
Re: for loop syntax difference
by toolic (Bishop) on Aug 28, 2009 at 14:00 UTC
    Here is a quote from the official Perl documentation (For Loops):
    And it's faster because Perl executes a foreach statement more rapidly than it would the equivalent for loop.
    I believe the "for" in the docs refers to a C-style loop (for($i=0;$i<10;$i++)) and the "foreach" refers to the more Perlish style (for $i (1..10)).

    To prove that it is faster for you, you would have to benchmark it, as someone else already explained to you in another forum. It is considered good etiquette to mention that you already asked this question elsewhere.

      I believe the "for" in the docs refers to a C-style loop (for($i=0;$i<10;$i++)) and the "foreach" refers to the more Perlish style (for $i (1..10)).

      Yes, however for and foreach are interchangeable since perl automatically detects which one to use.
        Also from the official Perl documentation (For Loops):
        The foreach keyword is actually a synonym for the for keyword, so you can use foreach for readability or for for brevity.
Re: for loop syntax difference
by bv (Friar) on Aug 28, 2009 at 14:15 UTC

    Yay benchmarks.

    #!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); cmpthese(-10, { forloop => sub { for (my $i = 0; $i < 500; ++$i) { my $x = 10*$i; } }, eachloop => sub { for my $i (0..499) { my $x = 10*$i; } }, }); __END__ Rate forloop eachloop forloop 5513/s -- -22% eachloop 7075/s 28% --
    $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;
Re: for loop syntax difference
by ikegami (Patriarch) on Aug 28, 2009 at 14:03 UTC

    for my $i (1..10) is much more readable than for (my $i=0; $i<10; $i++). In terms of efficiency, they should be comparable. They are both counting loops. The C-style loop should be a bit slower since it does more work on the Perl side.

Re: for loop syntax difference
by Sue D. Nymme (Monk) on Aug 28, 2009 at 14:26 UTC

    The documentation you're reading is out of date.

    Once upon a time, when you looped for (1..1000), perl would create a list of one thousand elements in memory, and then loop over that list. Looping C-style (for ($x=1; $x<=1000; $x++)) would not have the initial overhead of creating the list, nor the overhead of holding that list in memory. For short loops, like your 1-10 example, this was a trivial difference.

    In any case, this is no longer an issue. Perl no longer creates the throwaway list in memory; the two are basically equivalent now.

Re: for loop syntax difference
by Taulmarill (Deacon) on Aug 28, 2009 at 14:02 UTC
    for $i (1..10) creates a list with ten elements and iterates of that.
    for($i=0;$i<10;$i++) initializes $i with the value 0, then for every iteration of the loop it checks $i and increments if the check returns true or ends the loop.

      for $i (1..10) creates a list with ten elements and iterates of that.

      No, it doesn't. It's optimised into a counting loop. It increments the loop counter every pass rather than creating a list.