On 5/23/05, Peter Rabbitson <[EMAIL PROTECTED]> wrote:
> On Mon, May 23, 2005 at 01:40:08PM -0400, Zhenhai Duan wrote:
> > I tried hash (where the members of a group are joined with ":"), and hash
> > of hash. It happended that hash of hash is slower than single hash.
> >
> > Hash:
> > $groups{$g1} = "$member1:$member2";
> >
> > Hash of hash
> > $groups{$g1}{$member1} = 1;
> >
> > Method 1 is faster, even I need to do a split to get the members.
> 
> Can you post some code? Without it the above statement is not very credible
> to say the least.

The 1D approach seems to be approximately 3 times as fast (on x86
Linux). Anyone get different results?

#!/usr/bin/perl
use strict;
use warnings;

my @ra = ('a' .. 'z', 'A' .. 'Z');
my ($seqlen, $hashsize) = (4, 70000);

my (%oned, %twod) = ((),());
my (@l1, @l2) = ((),());

$|++;
print "generating hashes";
for my $i (1 .. $hashsize) {
    my $key1 = join('', @ra[map(int(rand($#ra))+1, 1 .. $seqlen)]);
    push @l1, $key1;
    my $key2 = join('', @ra[map(int(rand($#ra))+1, 1 .. $seqlen)]);
    push @l2, $key2;
    $oned{"$key1:$key2"}++;
    $twod{$key1}{$key2}++;
    print '.' if not $i % int($hashsize/10);
}
print "!\n";

# baseline
my ($su, $ss) = times;
for (1 .. $hashsize) { }
my ($eu, $es) = times;
my ($tu, $ts) = ($eu - $su, $es - $ss);
my $tt = $tu + $ts;
printf "%20s %5.2f %5.2f %6.2f\n", 'base', $tu, $ts, $tt;

# access test for 1d
($su, $ss) = times;
for my $i (0 .. $hashsize-1) {
    $oned{"$l1[$i]:$l2[$i]"}++
}
($eu, $es) = times;
($tu, $ts) = ($eu - $su, $es - $ss);
$tt = $tu + $ts;
printf "%20s %5.2f %5.2f %6.2f\n", '1D', $tu, $ts, $tt;

# access test for 2d
($su, $ss) = times;
for my $i (0 .. $hashsize-1) {
    $oned{$l1[$i]}{$l2[$i]}++
}
($eu, $es) = times;
($tu, $ts) = ($eu - $su, $es - $ss);
$tt = $tu + $ts;
printf "%20s %5.2f %5.2f %6.2f\n", '2D', $tu, $ts, $tt;

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to