Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Keep It Simple, Stupid
 
PerlMonks  

Hash references versus hashes: is there a performance hit?

by w-ber (Hermit)
on Mar 06, 2007 at 20:07 UTC ( #603502=perlquestion: print w/ replies, xml ) Need Help??
w-ber has asked for the wisdom of the Perl Monks concerning the following question:

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";

Comment on Hash references versus hashes: is there a performance hit?
Download Code
Re: Hash references versus hashes: is there a performance hit?
by jettero (Monsignor) on Mar 06, 2007 at 20:14 UTC

    I'm pretty sure those should turn out to be almost exactly the same. If speed is that important, perhaps you should look at assembly language or C or something? You can always import those routines into perl later using XS.

    You could check using Benchmark:

    Curiously, $h{hrmph} seems to actually be measurably faster, but by a very tiny amount.

    -Paul

Re: Hash references versus hashes: is there a performance hit?
by philcrow (Priest) on Mar 06, 2007 at 20:18 UTC
    The real difference comes when you pass the hash to a sub. Then, large hashes are serialized as lists, while references are just a single scalar.

    I'm not willing to guess what the performance of a simple lookup is. But, both of them have the hash the key which might be the dominant part of the process.

    Phil

Re: Hash references versus hashes: is there a performance hit?
by chromatic (Archbishop) on Mar 06, 2007 at 20:40 UTC

    It doesn't matter.

    Your program probably spends more time locating and loading modules off of disk than the time difference between accessing a hash by reference and directly.

Re: Hash references versus hashes: is there a performance hit?
by Anno (Deacon) on Mar 07, 2007 at 00:18 UTC
    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.

    You don't need a profiler, simple benchmarks will do, as jettero has remarked. The Benchmark module is easy to use, in particular its cmpthese() function. It is also the only reasonable answer to speed questions because the answer usually depends on the hard- and software environment. That is especially true when the expected difference in performance is small.

    Anno

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://603502]
Approved by jettero
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-04-19 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (482 votes), past polls