Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Is for(@$array_ref) construct optimized? YES!

by Aristotle (Chancellor)
on Jun 16, 2002 at 19:38 UTC ( [id://174959]=note: print w/replies, xml ) Need Help??


in reply to Is for(@$array_ref) construct optimized? YES!

You're preaching to the converted. :) If you use -MO=Deparse, you'll see that the for(;;) gets compiled to a while with a continue block, the latter being where the massive performance difference weighs in. Considering that the for(@array) form eradicates fence post errors from the root, there's rarely any good reason to use for(;;) in Perl.

Update: I forgot. Depending on your version of B::Deparse you may need increase the exposition level with the -x switch in order to see what things really look like to Perl under the hood.
$ perl -MO=Deparse,-x7 -e 'for($i=0;$i<=10;$i++){}' $i = 0; while ($i <= 10) { (); } continue { ++$i } -e syntax OK $ perl -MB::Deparse -e' print "$B::Deparse::VERSION\n"' 0.6 $ perl -v This is perl, v5.6.1 built for i386-linux [...]

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Is for(@$array_ref) construct optimized? YES!
by Weasel (Sexton) on Jun 16, 2002 at 19:53 UTC
    Sorry, I just checked a situation and can not confirm your sayings. Here is my log:
    D:\Work\PerlScripts\utl>perl -MO=Deparse -we "$x=[];for(@$x){}" $x = []; foreach $_ (@$x) { (); } -e syntax OK D:\Work\PerlScripts\utl>perl -MO=Deparse -we "$x=[];for(;;){}" Name "main::x" used only once: possible typo at -e line 1. $x = []; for (;;) { (); } -e syntax OK D:\Work\PerlScripts\utl>perl -MO=Deparse -we "$x=[];for($i=0;$i<=$#x;$ +i++){}" $x = []; for ($i = 0; $i <= $#x; ++$i) { (); } -e syntax OK
    This is probably version-dependent (which perl version you use?) and with less probablilty $^O-dependent (what is $^O in your case?)

    I think we'll find common denominator soon.

    update: after I saw your update I understood more and now I think we found that commond denominator that I've just mentioned. Thanks.

      On Linux with Perl5.6.0:
      $ perl -MO=Deparse -we '$x=[];for($i=0;$i<=$#x;$i++){}'
      $x = [];
      $i = 0;
      while ($i <= $#x) {
          ();
      }
      continue {
          ++$i
      }
      -e syntax OK
      
      Screamer is right - but thanks for preaching anyway, not all of us are 'converted' just yet. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        Aha! we see a difference between 5.6.0 and 5.6.1 with respect to this:
        D:\Perl56\bin>perl -MO=Deparse -we "$x=[];for($i=0;$i<=$#x;$i++){}" $x = []; $i = 0; while ($i <= $#x) { (); } continue { ++$i } -e syntax OK D:\>perl -MO=Deparse -we "$x=[];for($i=0;$i<=$#x;$i++){}" $x = []; for ($i = 0; $i <= $#x; ++$i) { (); } -e syntax OK
        Here I tried to compare 5.6.0 (I called it from 5.6.0 dir which is d:\perl56\bin in my computer) and 5.6.1 (it's in my $PATH)

        update I did update to call a snippet without compile errors; but explanation of results before and after update are same

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://174959]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found