A question of Style, was: SPUG: Sort an array question

Michael R. Wolf MichaelRunningWolf at att.net
Mon Dec 30 05:33:28 CST 2002


Tom Legrady <legrady at rogers.com> writes:

> >   "C-ish"      => sub { for ($i = 0; $i < @a; $i++for ($i = 0; $i < @a; $i++)    { $a[$i] = 0; } },
> >   "csh-ish"    => sub { foreach $i (0 .. $#a)          { $a[$i] = 0; } },
> >   "C cheat"    => sub { for ($i = 0; $i < a_len; $i++) { $a[$i] = 0; } },
> >   "csh nogen"  => sub { foreach $i (@a_indices)        { $a[$i] = 0; } },
> >   "csh direct" => sub { foreach $i (@a)                { $i     = 0; } },
> 
> > C-ish      58.6/s 
> > C cheat    73.0/s 
> > csh nogen  86.4/s 
> > csh-ish    97.4/s 
> > csh direct  131/s 


> So, at the extreme, we have a 2 1/4 / 1 improvement. ..... who
> cares, unless the code is somewhere that performance is crucial.

I care.  I now know something about Perl that I didn't before.  Seven
years into using the language, I learned something about the language
and about a profiling tool.  I choose to share it with the group
figuring that some folks would find instructive.  I'd already done the
work; sharing was cheap.

Of course, the numbers are meant to show the performance of an almost
_naked_ loop.  (I left something in the body because it appeared that
some Perl optomization was skewing my empty-loop results.)  The
_contents_ of a fleshed out loop are going to dominate the CPU time.
I wouldn't choose one loop over the other based *solely* on these
naked numbers.  I fact, it's unlikely that I'll change any loops in
the future based on these numbers.  I already use the "csh direct"
method, and regularly hilight the linguistic prowess of Perl by
showing beginning Perl classes why the C-ish loop introduces
unnecessary complexity, as you aptly point out in your comparison of
the CONCEPT and SYNTAX of a foreach loop.

It is my opinion, however, that the problem, as originally stated is
still more readable using $i and $j in a C-ish for loop since the
inner loop index is dependent on the outer loop index.  My attempts at
creating a foreach loop did not yeild a more readable solution.
Perhaps another's perspective will.

Despite it's 20% speed increase, I did *not* even entertain the
following refactoring to optomize the loop:

    for ( my($i, $a_len) = (0, scalar(@a))  ; 
          $i < $a_len                       ; 
          $i++ )
    {
        $a[$i] = 0;
    }

-- 
Michael R. Wolf
    All mammals learn by playing!
        MichaelRunningWolf at att.net


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list