Forum OpenACS Development: How to speed up this very slow loop?

Collapse
Posted by Janine Ohmer on
I have some code (not mine, though I'm responsible for it now) which goes through multirow A, and for each record it tries to find a corresponding row in multirow B.  It does this via the brute force method - for each new row in multirow A, it starts over at the beginning of multirow B and checks rows until it finds a match, via two nested for loops.  A match is determined by comparing the user_id column in both multirows.

This code is running very slowly - almost 10 minutes for about 900 users.  Granted I'm working on a pokey system, but still... this is not acceptable.  What would be the best (read easiest to code) solution to speed this up?  I have looked at the array commands but they don't look like they will help me here.

The actual code, in case the above was not clear, looks like this:

for {set i 1} {$i <= [multirow size phb_all]} {incr i} {
    set found_p 0
    for {set j 1} {$j <= [multirow size phb_person]} {incr j} {
        if {[set phb_all:${i}(def_user_id)] == [set phb_person:${j}(user_id)]} {
            set found_p 1
            <set up some variables>
            break
        }
    }
    if (!$found_p) {
        <set up some other variables>
    }
}

Collapse
Posted by Jeff Davis on
you should probably generate a tcl array to map phb_all to phb_person which I think would be a lot faster (alternatively
make a list of each, sort it, and walk through in order).
Not clear which of those two alternatives would be faster but
certainly both are better than the O(n^2) lookup here.