Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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]
[Corion]: 1nickt: No, I'm using it with Windows 10, but with the 4k display, yes. It's a very nice build IMO, and relatively sturdy. Also, it got 8 or 9 on the ifixit repair scale, which I consider good
[Corion]: ... especially as I expect to have to replace the battery set in two or three years' time
[1nickt]: Nice. So happy that Dells are still user-repairable. Unusual these days.
[1nickt]: I will certainly not be buying any more Macs.
[Corion]: 1nickt: Yeah, or at least, I hopoe so ;)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2017-03-27 13:15 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.