Android Question Resumable Sub Execution Speed Vs. Regular Sub

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
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:
B4X:
'  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?
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Good catch on the release mode, since I'm using an actual Galaxy S5 for testing I didn't think to try debug vs. release.

In debug mode I see the massive delay between the procedures, in release mode, both procedures produce the same result. I guess I'll just leave it in release mode while I develop o_O.

Thank you, @stevel05!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is a debug-only issue. It is related to an important optimization that is currently not applied to resumable subs.

In some cases you can move the "heavy code" to a different sub and call it from the resumable sub. This way it will run fast in debug mode. Make sure to clean the project before you test it.
 
Upvote 0
Top