in reply to Re^2: Iterating through Two Arrays. Is there a better use of memory?
in thread Iterating through Two Arrays. Is there a better use of memory?

So, you're saying. If I do just at AR suggested, I will have a linear growth rate instead of an exponential?

  • Comment on Re^3: Iterating through Two Arrays. Is there a better use of memory?

Replies are listed 'Best First'.
Re^4: Iterating through Two Arrays. Is there a better use of memory?
by AR (Friar) on Oct 13, 2011 at 17:17 UTC

    Almost definitely. The worst case scenario with hashes is if every hashed key collides. In that case, you may as well be using an array.

Re^4: Iterating through Two Arrays. Is there a better use of memory?
by DentArthurDent (Monk) on Oct 13, 2011 at 17:21 UTC
    That's exactly what I'm saying. Your first algorithm has quadratic growth (not exponential). The second algorithm has linear growth. It's a linear cost in the smaller array to load up the hash and then a linear cost in the larger array to do all the look-ups.

    By the way: exponential growth would be O(n^m). Your original algorithm is polynomial, or quadratic to be precise O(n^2).

    Ben
    ----
    My mission: To boldy split infinitives that have never been split before!
      Note that it's only the runtime that's quadratic (or, to be precise, O(n*m)). The memory usage of the algorithm is bounded by O(n+m).