[mplspm]: In a sorted list [better]

John J. Trammell trammell at trammell.dyndns.org
Tue Mar 26 18:45:30 CST 2002


On Wed, Mar 27, 2002 at 09:44:39AM +1100, Ken Williams wrote:
> Huh, funny.
> 
> Well, it might have something to do with the hash keys.  Numeric hash 
> keys (especially sequential ones) notoriously trip up the perl hashing 
> algorithm, sometimes making everything go into a single bucket.  I'm not 
> sure whether this has been patched in recent perls or not.  Maybe try 
> with random words from /usr/dict/words ?

Trying with randomly-generated nonsense words doesn't change the
flavor of the outcome (results posted below).

> The other thing is that perl maintains a global table of hash keys so 
> that it doesn't have to re-hash a key that it's seen elsewhere in a 
> different context.

I wonder if the flatness of the timing is related to that.

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

sub __map
{
        my ($a) = @_;
        my %h = map { $_, 0 } @$a;
        return \%h;
}

sub __slice
{  
        my ($a) = @_;
        my %h;
        @h{ @$a } = ();
        return \%h;
}

foreach my $size ( 10, 100, 1000, 10_000 )
{
        warn "$size-element array\n";
        my @x = genarray($size); 
        warn "sample: @x[0..5]\n";
        timethese( 1_000_000, {
                map =>   q[ my $foo = __map(\@x) ],
                slice => q[ my $bar = __slice(\@x) ],
        });
}

sub genarray
{
        my $size = shift;
        my @a = ('a' .. 'z');
        my @out;
        for (1 .. $size)
        {
                push @out, join("", map $a[rand(@a)], 0 .. 2+rand(5));
        }
        return @out;
}

__END__

10-element array
sample: xfjpra awhtc kyu uylhkj ogctixb nzwj
Benchmark: timing 1000000 iterations of map, slice...
       map:  4 wallclock secs ( 4.20 usr +  0.01 sys =  4.21 CPU) @ 237529.69/s (n=1000000)
     slice:  5 wallclock secs ( 4.35 usr +  0.00 sys =  4.35 CPU) @ 229885.06/s (n=1000000)
100-element array
sample: hcc lggly hay ygdqh wlscd afdos
Benchmark: timing 1000000 iterations of map, slice...
       map:  3 wallclock secs ( 4.08 usr +  0.00 sys =  4.08 CPU) @ 245098.04/s (n=1000000)
     slice:  4 wallclock secs ( 4.39 usr +  0.00 sys =  4.39 CPU) @ 227790.43/s (n=1000000)
1000-element array
sample: vlkqrxe oebi msmc ghz xmkd vlcbrs
Benchmark: timing 1000000 iterations of map, slice...
       map:  3 wallclock secs ( 4.12 usr +  0.00 sys =  4.12 CPU) @ 242718.45/s (n=1000000)
     slice:  4 wallclock secs ( 4.37 usr +  0.00 sys =  4.37 CPU) @ 228832.95/s (n=1000000)
10000-element array
sample: evpo vsyprxl eidu bgqilvs piex zhb
Benchmark: timing 1000000 iterations of map, slice...
       map:  3 wallclock secs ( 4.15 usr +  0.00 sys =  4.15 CPU) @ 240963.86/s (n=1000000)
     slice:  4 wallclock secs ( 4.45 usr +  0.00 sys =  4.45 CPU) @ 224719.10/s (n=1000000)



--------------------------------------------------
Minneapolis Perl Mongers mailing list

To unsubscribe, send mail to majordomo at pm.org
with "unsubscribe mpls" in the body of the message.



More information about the Mpls-pm mailing list