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

Hi.

I was just fiddling around with some Perl and DBM, experimenting a bit, when I came across an interesting question. I was writing a subroutine which connects to different largish DBM files depending on the arguments passed. This sub may need to be called several times before the script is through, and may connect to a number of different DBM files, but may also connect to some repeatedly. I am somewhat concerned about performance, so I concocted two possible approaches for this subroutine... however I am not too familiar with DBM performance issues, so there may be a better way than what I'm thinking.

Option A: I keep refs to the tied hashes in a package master hash as I tie() them. Whenever I invoke the subroutine in question, I check that master hash to see if I've already tied to that DBM file. If so, I use the existing ref; if not, I create a new ref and stick it in the hash for next time. Then, I just untie() everything at the end. This approach is best if the tie() function causes a performance hit, it minimizes the number of times it must be called.

Option B: I tie-on-the-fly, simply tying the hash at the beginning of the subroutine, and untie() at the end. This approach assumes that tie() does not cause a significant performance hit, but keeping the ties hashes in memory does.

I know that there's not always a best answer for these things, but I'd like to tap anybody's experience who has some to spare.

As a related question, must one explicitly untie() a hash, or will garbage collection take care of that? I'm sure it's good form to untie(), so I will... I'm just curious.

Thanks,

Hot Pastrami

Replies are listed 'Best First'.
(tye)Re: Permance regarding tying hashes to DBM
by tye (Sage) on Jan 26, 2001 at 01:02 UTC

    Call this an educated guess, but I think there is a performance hit for tie and untie to a DBM and I don't see much performance hit at all for keeping the tied references around.

    Go with Option A!

            - tye (but my friends call me "Tye")