in reply to Should we bother to save hash lookups for performance?

It makes sense when your Benchmark tests say it's a problem but not prior. Consider that you're also making a copy of the data which might have it's own implications (unless you get Copy-On-Write (unless that's only a perl6ism)). I'd almost consider this sort of thing right along with the my ($a); vs my $a; optimization. I'd do it for notational convenience but not much else (unless you really do have thousands of entries in your hash and you really, really, really, really care about the pico seconds you're losing ...).

Update: There is a difference between my ($foo) and my $foo - one has to include an extra OPCODE or two to coerce the scalar into list context while the other is already there. I'll leave it to you and B::Terse to figure out which one is "cheaper".

__SIG__ printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;

Replies are listed 'Best First'.
Re: Re: Should we bother to save hash lookups for performance?
by nothingmuch (Priest) on Oct 18, 2002 at 18:52 UTC
    I would like to expand on the reasons for caching. If you are farmiliar with hashing, then you'll probably know that the key is processed in order to generate an index. Should this index be taken, most hash implementations will use a sub structure (probably a linked list, who's search time is O(N)) to store duplicate hashes of keys.
    Perl hashes probably differ, I don't know how, but I estimate the principal is similar.
    Perl hashes (not tied ones) can be evaluated in string context to see how many buckets are used and how many are allocated, for that particular hash. If you seem to be using a very small number of buckets out of the allocated ones, than you probably have an average lookup time slightly larger than O(1). Should that be the case, i'd suggest caching.

    Even so, I think that diotalevi's advice to use Benchmark is probably the best you'll get...

    Good luck!

    -nuffin
    zz zZ Z Z #!perl
Re: Re: Should we bother to save hash lookups for performance?
by dlink (Novice) on Oct 18, 2002 at 17:28 UTC
    Thanks. What ever looks clear notation-wize then, is the keeper.