First, anytime you are curious about a performance question, you should Benchmark. Nothing gives you truth like experiment.

#!/usr/bin/perl -w use strict; use Benchmark qw':all :hireswallclock'; my %the_hash = ( empl_john_id => 13, empl_john_position => 'slave', empl_bob_id => 0, empl_bob_position => 'manager', client_fred_id => 2, client_fred_phone => 12345, client_goldman_id => 0, client_goldman_phone => 666 ); my %the_hash2 = ( empl => { john => { id => 13, position => 'slave' }, bob => { id => 0, position => 'manager' } }, client => { fred => { id => 2, phone => 12345 }, goldman => { id => 0, phone => 666 } } ); cmpthese(10**6, { 'emulation' => sub {$the_hash{client_goldman_phone}++ }, 'HoH' => sub { $the_hash2{client}{goldman}{phone}++ }, });

yields

(warning: too few iterations for a reliable count) Rate HoH emulation HoH 1883239/s -- -27% emulation 2564103/s 36% --

In a literal sense, multidimensional hash emulation will be faster than using a hash of hashes (at least for this test). This is mostly because there is no need for dereferencing the sub elements (I think). However, this is highly unlikely to be the bottleneck in your code, and multidimensional hash emulation is considered poor form because it makes code harder to understand, and hence harder to debug and maintain. This technique's usage mainly predates the introduction of proper references into Perl.

For a discussion of hash performance, see A short meditation about hash search performance.


In reply to Re: multidimensional hash emulation vs hash of hashes by kennethk
in thread multidimensional hash emulation vs hash of hashes by dexahex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.