Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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


In reply to Re: how do I efficiently remove one hash from another? by rjt
in thread how do I efficiently remove one hash from another? by perltux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (8)
    As of 2014-11-27 06:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (180 votes), past polls