smiffy has asked for the wisdom of the Perl Monks concerning the following question:

Considering the following two ways of doing the same thing, which is the most efficient in terms of how much work the Perl interpreter has to do, or does it make no difference?

Method 1

Pass a hashref to the subroutine.

$hr->{'foo'}='bar'; do_something_mutley($hr); sub do_something_mutley { my $lhr=shift; my $baz=$lhr->{'foo'}; # Various stuff would happen here. return ($baz); }

Method 2

Get the value of the key from the hashref and pass it to the subroutine.

$hr->{'foo'}='bar'; do_something_mutley($hr->{'foo'}); sub do_something_mutley { my $baz=shift; # Various stuff would happen here. return ($baz); }

Please note that the above are code fragments - strict and warnings would be in use.

I really don't think that I'll lose any (or at least not much) sleep over a few CPU cycles but it would be nice to know if there is a difference in overhead between the two methods.

Replies are listed 'Best First'.
Re: Calling a subroutine - which is most efficient?
by GrandFather (Saint) on Oct 25, 2008 at 22:45 UTC

    Think in terms of overall work done. In both cases you are passing a scalar value into the sub so the parameter handling is identical. In one case you dereference when making the call. In the other case you dereference inside the sub. In both cases you dereference once per call - no difference there.

    As with many decisions about how to do stuff, the first way you try should be dictated by what is most maintainable - what form conveys the intent most clearly and is least prone to accidental breakage. Down the track when the code is working correctly, if you find it is too slow, then you may need to revisit code with a view to making it faster, but only after profiling and having a really good understanding of where the time is actually spent.

    How many milliseconds per call of overhead are worth a half hour of your beer drinking time?


    Perl reduces RSI - it saves typing
Re: Calling a subroutine - which is most efficient?
by dragonchild (Archbishop) on Oct 26, 2008 at 00:46 UTC
    Interpreter efficiency is identical in both cases. What's more interesting to me is programmer efficiency, specifically maintainability. And, there's no easy answer. In a Perl context, I guess it's whether or not your program is more OO (where you would pass the blessed hashref) or more functional (where you would pass the value in order to be less coupled). It's an interesting thought exercise.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Calling a subroutine - which is most efficient?
by smiffy (Pilgrim) on Oct 26, 2008 at 04:28 UTC

    Thanks for your thoughts, folks. It's nice to know that the two methods are equal and I now have some food for thought too.

    I actually need to use a mixture of the two methods - sometimes the subroutine will be changing another key in the same hashref, thus the reference will need to be passed as per the first method. Other times the key in my main hashref may point to a different value and thus I don't want it associated at that point - so the second method would apply.

    @GrandFather - point taken. Wouldn't want to waste VAT (Valuable Ale Time) for a few ms that nobody will ever notice. I have to confess that I do 'waste' a certain amount of time going back and 'fixing' working code (at my expense, not the client's) - which could be better spent on other things (including beer time). But then this tidying up generally takes the form of simplification and making things more consistent, which does make the code easier to follow when I come back to it later.

    @Anonymous Monk - yes, the second method makes the subroutines easier to re-purpose as they aren't always going to be working on the same hashref. I just need to apply the first method when the subroutine actually does stuff to the referenced hash.

    @dragonchild - I tend away from OO because my mind tends to work best the other way. (Hey, otherwise I'd be at JavaMonks - just joking ;-)) So, the tendency would be to passing values rather than hashrefs, but there are times when I would be wanting to do stuff to key values in the referenced hash so would duly pass the hashref. I know exactly what's going on in both cases due to my prolific use of comments, so no real issues of maintainability. Nice to know that neither method is 'better' than the other though.

      It's worth keeping in mind that maintainability is very subjective. We all agree that certain levels of obfuscation are not maintainable, but what exactly is maintainable really depends a lot on "for whom?"

      Like you, I prefer to eschew OO (at least in Perl), but that loops back to the "for whom?" If I write code for a total newbie to maintain but only have gurus on my team, I'm essentially burning a lot of time and effort to accomplish nothing more than annoying my team-mates...

Re: Calling a subroutine - which is most efficient?
by Anonymous Monk on Oct 25, 2008 at 22:51 UTC

    No difference in efficiency.

    Think instead about information flow and modularization and the likelihood of reuse in a slightly different context. The first sub knows more than the second sub - it knows it has to look up a certain fixed key, namely 'foo'. What if the various stuff turned out to be relevant in another situation, but where $baz didn't have this association with 'foo'? That's why my hunch is to prefer the second version.

    But efficiency is not the issue.