[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Re: Neat or horrid?



Ilmari Karonen wrote:

> > >     sub sum {@_ ? shift () + &sum : 0}
> 
> How about:  sub sum {@_ && shift() + &sum}

You've saved a couple of strokes but have a slightly
more confusing idiom, and it still takes longer than
iterating with a lexical.

I used the suse on alpha at www.compaq.com/alphaserver/beowulftestdrive
and I appear to have had the box to myself during the benchmark.

One and a half percent slowdown die to using && rather than ? is
mystifying to me.  Could it be that && is slower on 5.005_03?


use Benchmark;

sub sum1 {@_ ? shift () + &sum1 : 0}
sub sum2 { eval join '+',@_, }
sub sum3 {my$x;while(@_){$x+=pop};$x}
sub sum4 {@_ && shift() + &sum4}

$RESULT = sum3(273 .. 732 );
sub control {$RESULT};

timethese(5500,{
  Abigail_Recursive  => sub {sum1(273 .. 732 ) == $RESULT or die "math error"},
  NonFree_Evaluation => sub {sum2(273 .. 732 ) == $RESULT or die "math error"},
  The_X_Whiles       => sub {sum3(273 .. 732 ) == $RESULT or die "math error"},
  Ilmari             => sub {sum4(273 .. 732 ) == $RESULT or die "math error"},
  control            => sub {control(273 .. 732 ) == $RESULT or die "math error"}
});

Benchmark: timing 5500 iterations of Abigail_Recursive, Ilmari, NonFree_Evaluation, The_X_Whiles, control...
 Abigail_Recursive: 10 wallclock secs (10.40 usr +  0.00 sys = 10.41 CPU)
            Ilmari: 10 wallclock secs (10.53 usr +  0.00 sys = 10.53 CPU)
NonFree_Evaluation: 24 wallclock secs (25.08 usr +  0.00 sys = 25.08 CPU)
      The_X_Whiles:  4 wallclock secs ( 3.63 usr +  0.00 sys =  3.63 CPU)
           control:  0 wallclock secs ( 0.17 usr +  0.00 sys =  0.17 CPU)



-- 
                          David Nicol 816.235.1187 nicold@umkc.edu
                          find / -name core|perl -ne'chomp;unlink'

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe