Quoting Robin Houston ([EMAIL PROTECTED]):
> [robin@dev hash-bench]$ perl benchmark-hasharray.pl 2E5
> Benchmark: timing 2E5 iterations of array lookup, hash lookup...
> array lookup: 42 wallclock secs (43.07 usr +  0.00 sys = 43.07 CPU)
> hash lookup: 54 wallclock secs (50.68 usr +  0.01 sys = 50.69 CPU)

On the other hand ...

use Benchmark;

my(%hash, @array);
@array = (0 .. 999);
@hash{@array}=();


timethese(1E4, {
                "hash" => \&hash,
                "array" => \&array
               });

sub hash {
    for (my $c=0; $c<1000; ++$c) {
        ++$hash{$c};
    }
}

sub array {
    for (my $c=0; $c<1000; ++$c) {
        ++$array[$c];
    }
}

Benchmark: timing 10000 iterations of array, hash...
     array: 12 wallclock secs (11.86 usr +  0.00 sys = 11.86 CPU)
     hash: 64 wallclock secs (64.04 usr +  0.00 sys = 64.04 CPU)
           
Strangely, adding a "use integer;" line to the above gives:

Benchmark: timing 10000 iterations of array, hash...
     array: 10 wallclock secs ( 9.84 usr +  0.00 sys =  9.84 CPU)
     hash: 17 wallclock secs (16.86 usr +  0.00 sys = 16.86 CPU)
           
which suggests to me that stringifying doubles is an expensive operation.

Still, I'd have to agree that at least in Perl the major benefit of arrays
over hashes is memory consumption, not speed. To forestall the inevitable
protests, I have repeatedly been in situations where memory consumption was
a major issue over the past 10 years, most recently with a Sun server (do
you know how much they charge for RAM for Suns?), and I don't expect such
situations to go away any time soon. Even when memory is plentiful, when
building deep data structures in Perl the overhead of hashes starts to hurt
pretty quickly.

Adam

-- 
Adam Rice -- [EMAIL PROTECTED] -- Blackburn, Lancashire, England

Reply via email to