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

RE: [FWP] Moment of Zen



> From: Uri Guttman [mailto:uri@sysarch.com]
> Sent: Thursday, September 30, 1999 21:33
> To: rick.delaney@home.com
> Cc: fwp@technofile.org
> Subject: Re: [FWP] Moment of Zen
> 
> >>>>> "RD" == Rick Delaney <rick.delaney@home.com> writes:
> 
>   RD> Uri Guttman wrote:
>   >> 
>   >> well it helps to compare apples to macintoshes! a proper foreach
>   >> statement is a very different animal than a foreach statement
modifier.
>   >> 
>   >> so your benchmarks are moot. map and foreach modifiers are know
be be of
>   >> similar speed and both are generally faster than foreach
statements.

I don't see why a 'foreach' modifier should be faster than a 'foreach'
loop.

>   RD> Well, now the burden of proof has shifted to you.  Feel free to
modify
>   RD> the benchmark to prove this.  Or offer up some other proof.
> 
> i am feeling lazy tonight. maybe you can goad larry into one.

Earlier, I simply compared hash slices with foreach loops, because I
thought the former would be significantly faster.  That presumption was
the basis for my earlier statement.  I was quite wrong.
 

#!/usr/local/bin/perl -w           
use strict;
use Benchmark;

my %sites;
my $w = shift;
my $s = 26 ** $w;
@sites{ ('a' x $w) .. ('z' x $w) } = 1 .. $s;
print "Testing $s elements\n";

timethese 1 << shift, {
    Slice => sub {
        my %shares;
        @shares{keys %sites} = values %sites;
    },
    For => sub {
        my %shares;
        $shares{$_} = $sites{$_} for keys %sites;
    },
};

-- 
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com 

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