I'm coding a guessing-game project where I have a finite set of integer value possibilities to iterate through and I'm seeing something odd. I made a sub that goes through the integer item set (46,656 at most) eliminating non-matching items based on a comparator.
When the sub is just a tight loop that returns the result, it takes 4-7 seconds to execute (during which time the app is completely unresponsive, obviously). If I make an exact copy of the block that has a sleep(0) before the loop start and raise an event afterwards via callsubdelayed (no sleep inside the loop at all) it takes upwards of six minutes and change to execute:
' App started
clsAI.LoadPatterns: Combintaions(46656) vs. Map(46656).
PROCESS COMPLETE: NewGame
' --- Tight loop first ----
Guess #1 took 0:04. Result: 0 exact & 3 matches.
Eliminated 44404 possibilities, 2252 remaining.
' Took four seconds and change
' --- app stopped and restarted, then resumable sub loop used
Guess #1 took 6:58. Result: 1 exact & 2 matches.
Eliminated 39818 possibilities, 6838 remaining.
' Took nearly seven minutes!
I cut-and-pasted the code block so I know the only difference between the two procedures is the Sleep(0) before the loop and the CallSubDelayed after the loop completes instead of Return Result. Originally I had the sleep inside the loop with an "index mod 500 = 0" throttle on it, and experienced this behavior so I took that out completely and put a sleep before the loop just make sure the sub was resumable.
Why would this change produce such a vast performance difference? And, if this is behavior as intended, would the deprecated "DoEvents" in the first method allow the UI to be responsive without the performance hit?