vrk 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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash references versus hashes: is there a performance hit?
by philcrow (Priest) on Mar 06, 2007 at 20:18 UTC | |
|
Re: Hash references versus hashes: is there a performance hit?
by jettero (Monsignor) on Mar 06, 2007 at 20:14 UTC | |
|
Re: Hash references versus hashes: is there a performance hit?
by chromatic (Archbishop) on Mar 06, 2007 at 20:40 UTC | |
|
Re: Hash references versus hashes: is there a performance hit?
by Anno (Deacon) on Mar 07, 2007 at 00:18 UTC |