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

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! :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005822]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-06-25 10:24 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.