Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: how do I efficiently remove one hash from another?

by rjt (Deacon)
on Nov 27, 2012 at 09:28 UTC ( #1005822=note: print w/ replies, xml ) Need Help??


in reply to how do I efficiently remove one hash from another?

If you are concerned about looping through the values (as is common and necessary in all the ways to do what you've asked), is there any other way you can build up or maintain the data in the first place? Whenever I find an expensive operation in one of my programs, it's always worth going back to the data representation itself to see if there is a more efficient way of maintaining the data.

For instance, if the hash keys do not change often, and you are more concerned about CPU than memory, separately maintaining %hash1_minus_hash2 might be better. Your "write" operation takes twice as long (still O(1), just with a higher constant), but your O(n) loop is now O(1), which can be a huge win.

Also, if %hash1 is not more than twice as big as %hash2, and you're going to loop through %hash1 for the next step of your algorithm anyway (and you do this at most once per delete cycle), a construct like this might actually be more efficient:

for (keys %hash1) { next if exists $hash2{$_}; # ... your main loop code here }

There are of course many additional strategies that may be a better fit, but perhaps these two will spark an interest to take the problem up a level.

And, it's entirely possible there's nothing else you can do in your case. If that's true, cheers anyway! :-)


Comment on Re: how do I efficiently remove one hash from another?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005822]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-07-12 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (241 votes), past polls