[Phoenix-pm] Hash performance

Michael Friedman friedman at highwire.stanford.edu
Fri Jun 9 20:11:15 PDT 2006

Wait -- for #2 it sounds like you build the hashes and then write  
them out to a file (in some other script, I assume), followed by this  
script using 'require' to load the previously written files. Is that  

I would bet, then, that the extra memory and slowness comes from  
accessing the filesystem. Once you require a file, perl basically eval 
()s it into the current context -- doing just about exactly what you  
do when you build the hash in the first place. :-) Scott could  
probably explain the details, I'm just going on a guess.

Personally, I think you did the right thing by benchmarking it. Now  
you know for sure which way is better and you can just rebuild it  
when you use it.

If you want to save the hashes in a file, you might want to check out  
the GDBM, MLDBM, or TDB (a new really fast one) database modules.  
(GDBM_File, MLDBM + Tie::MLDBM, TDB_File) They all tie to a hash and  
let you manage the persistent storage without any effort whatsoever.

-- Mike

On Jun 9, 2006, at 5:38 PM, Metz, Bobby W, WWCS wrote:

> 	This is kind of a follow-up question to my multi-level hash
> post.  Everything I've been reading on-line about how hashes work  
> leads
> me to conclusions that don't seem to pan out in reality, e.g.
> pre-defining the # of hash buckets to increase performance on large  
> data
> sets.  At least, I thought +40K records would be considered large...no
> jokes please.
> 	So, here's what I've observed using two methods to load +40K
> records into a single level hash.  I have always used method #1 as I
> learned it that way years ago but would love some thoughts around
> whether method #2 might be superior somehow as I know a lot of folks
> that do it that way instead.
> Method 1
> + Dynamically build hash from data file at run time.
> + Program load is consistently 3 seconds faster than Method 2.
> + Used 13M of memory to hold the records.
> Method 2
> + Used pre-built hashes loaded via "require".
> + Program load is consistently 3 seconds slower than Method 1.
> + Used 36M of memory to hold the records.
> 	Any of you know the inner workings of hashes enough to explain
> the difference?  I think the memory increase might have something  
> to do
> with "require" mucking with the usual shared hash table used by perl,
> possibly forcing two copies.  But, that's just an uneducated guess.
> There was no discernable difference in output performance using a  
> small
> test set against the +40K records, only the initial program load and
> total memory consumption.
> Thoughts?
> Thanks,
> Bobby
> _______________________________________________
> Phoenix-pm mailing list
> Phoenix-pm at pm.org
> http://mail.pm.org/mailman/listinfo/phoenix-pm

Michael Friedman                     HighWire Press
Phone: 650-725-1974                  Stanford University
FAX:   270-721-8034                  <friedman at highwire.stanford.edu>

More information about the Phoenix-pm mailing list