in reply to Re: Common elements of a hash of hashes
in thread Common elements of a hash of hashes

Your results are garbage.

1) The %HoH you pass as an argument is a global from some package (main?), not the my %HoH which is not visible to that code when its compiled by Benchmark. Instead of
Perl_Mouse => 'perl_mouse(\%HoH)',
use
Perl_Mouse => sub { perl_mouse(\%HoH) },
to avoid this problem.

2) The first call of the first function will remove the uncommon elements. For every subsequent call to that function and to the other functions, %HoH will only contains keys common to all hashes. You need to deeply copy %HoH before passing it to the functions.

Overall, I don't expect a major difference in speed between the different versions. Perl Mouse's will probably be the slowest (since it does all its looping in Perl, where as the others do it in C), but 1) it uses minimal memory, and more importantly, 2) it's the easiest to read. Using foreach values instead of while each will probably speed it up at the expense of memory.

Replies are listed 'Best First'.
Re^3: Common elements of a hash of hashes
by Fang (Pilgrim) on Oct 07, 2005 at 20:53 UTC

    The first call of the first function will remove the uncommon elements. For every subsequent call to that function and to the other functions, %HoH will only contains keys common to all hashes. You need to deeply copy %HoH before passing it to the functions.

    Alright, I felt this would be an issue, but thought I had resolved it by passing a reference to each sub and then dereferencing it. Unfortunately, I missed the fact that the values of the hash were references, which would modify the original values in place... I plead guilty.

    To deeply copy the structure, I've just read about the dclone function from Storable.pm, I believe this is what's needed.

    I'll rewrite the small script in hope it can be more meaningful and useful than what I previously posted.