Honourable Monks,

I often ponder the performance of the code I am writing, but I do this at a relatively high, algorithmical level. I rarely dig into the intricate world of instructions (be it machine language or Perl bytecode). Recently a question has been forming in my mind: is there a performance hit if you use hash references instead of hashes? Allow me to elaborate.

%h = qw( ... ); $h = \%h; # Accessing the hash and the hash reference in a tight loop. for ( ... ) { my $frobnitz = $h{frobnitz}; my $quark = $h->{quark}; ... }

Is the latter hash access slower than the former, and if so, how much? Does either consume more memory than the other? Should I be aware of other possible overhead? If I have a complex data structure (HoHoHo...), would I be able to squeeze off some performance from using real hashes instead of hash references?

This is not a burning question, because I very rarely write code in Perl where I would need to mind these things. However, I'm curious.

I know I could possibly test this myself with a profiler, but I have yet to read how to use the Perl profiler, or even to figure out which would be a good test scenario.

Thank you for your patience.

--
print "Just Another Perl Adept\n";


In reply to Hash references versus hashes: is there a performance hit? by vrk

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.