Why does my Loop end after 11 Loops?
#61
@Dimster Recursion isn't going to help you one whit.  Let me highlight what the problem is for you, in a nutshell:

Code: (Select All)
SCREEN _NEWIMAGE(800, 600, 32)
DIM AS _UNSIGNED LONG a, b


t# = TIMER + 10
DO
    b = b + 1
    PRINT b
LOOP UNTIL TIMER > t#

a = 44 * 44 * 44 * 44 * 44 * 44
PRINT a '2,961,346,560
PRINT USING "################.## seconds to print #,###,###,### times on the screen."; (a / b) * 10, a 'times 10 as we counted for 10 seconds
PRINT USING "################.## minutes to print #,###,###,### times on the screen."; (a / b) / 6, a
PRINT USING "################.## hours to print #,###,###,### times on the screen."; (a / b) / 360, a

PRINT "Press <ANY KEY> for a different test by Steve(tm)!"
SLEEP

DIM AS _UNSIGNED LONG i, iLimit, count, count1
iLimit = 2961346560
t# = TIMER + 10
FOR i = 0 TO iLimit - 1
    count = count + 1
    printNums i
    IF TIMER > t# THEN EXIT FOR
NEXT
PRINT USING "################.## seconds to print #,###,###,### times on the screen."; (a / count) * 10, a
PRINT USING "################.## minutes to print #,###,###,### times on the screen."; (a / count) / 6, a
PRINT USING "################.## hours to print #,###,###,### times on the screen."; (a / count) / 360, a


PRINT "Press <ANY KEY> for a final test by Steve(tm)!"
SLEEP


t# = TIMER + 10
count1 = 0
FOR TNa = 1 TO 44
    FOR TNb = 2 TO 45
        FOR TNc = 3 TO 46
            FOR TNd = 4 TO 47
                FOR TNe = 5 TO 48
                    FOR TNf = 6 TO 49
                        PRINT TNa; TNb; TNc; TNd; TNe; TNf
                        count1 = count1 + 1
                        IF TIMER > t# THEN GOTO finishedfors
                    NEXT
                NEXT
            NEXT
        NEXT
    NEXT
NEXT

finishedfors:
PRINT USING "################.## seconds to print #,###,###,### times on the screen."; (a / count1) * 10, a
PRINT USING "################.## minutes to print #,###,###,### times on the screen."; (a / count1) / 6, a
PRINT USING "################.## hours to print #,###,###,### times on the screen."; (a / count1) / 360, a
PRINT "**************"
PRINT "**************"
PRINT "Final results:"
PRINT "Straight print in hours:"; (a / b) / 360
PRINT "Math print in hours:"; (a / count) / 360
PRINT "For print in hours:"; (a / count1) / 360
PRINT
PRINT
PRINT "And, to do the same calculations WITHOUT a PRINT statement?"
PRINT "...."

t# = TIMER + 10
count2 = 0
FOR TNa = 1 TO 44
    FOR TNb = 2 TO 45
        FOR TNc = 3 TO 46
            FOR TNd = 4 TO 47
                FOR TNe = 5 TO 48
                    FOR TNf = 6 TO 49
                        'PRINT TNa; TNb; TNc; TNd; TNe; TNf
                        count2 = count2 + 1
                        IF TIMER > t# THEN GOTO finishedfors2
                    NEXT
                NEXT
            NEXT
        NEXT
    NEXT
NEXT

finishedfors2:
PRINT "NO printin hours:"; (a / count2) / 360&&


SUB printNums (nValue AS _UNSIGNED LONG)
    STATIC AS _UNSIGNED LONG n, n1, n2, n3, n4, n5, n6
    DIM AS _UNSIGNED LONG r, r1, r2, r3, r4, r5, r6
    IF NOT n THEN
        n = 1
        n1 = 44
        n2 = n1 * 44
        n3 = n2 * 44
        n4 = n3 * 44
        n5 = n4 * 44
        n6 = n5 * 44
    END IF
    r = nValue
    r5 = r MOD n6
    r5 = (r MOD n5) \ n4
    r5 = (r MOD n4) \ n3
    r5 = (r MOD n3) \ n2
    r5 = (r MOD n2) \ n1
    r6 = r MOD n1
    PRINT r1 + 1, r2 + 2, r3 + 3, r4 + 4, r5 + 5, r6 + 6
END SUB


Now you've got 6 loops running 44 times each.  (loop 1 is 1 to 44, loop 2 is 2 to 45, loop 3 is 3 to 46, but they're all running 44 times each -- just with a different start and end value)

That's 2,961,346,560 times total.  (44 * 44 * 44 * 44 * 44 * 44)

The above demo runs 3 times for us, and tries to calculate how long it'd take to run the full count, if we let it.  

First, it does a simple count from 1 to 2,961,346,560 and sees how high it can count in 10 seconds.  It then uses that 10 second count to estimate the time to do the whole count.

Then it tries to bypass the nested looping and do some math for its counting.  It sees how high it can count in 10 seconds and then estimates us a finished time.

Then it goes to the default method which you posted, and does that same style counting and estimation, and then it displays the various results for comparison.

And while you're looking at the results and seeing that NONE of the changes really make much of a difference, it runs in the background and tries to calculate how long it'd take to do the process WITHOUT any PRINT statement being involved...

The final results on my laptop (which was also trying to do windows updates in the background) were:

   


We're talking 5 or 6 days from start to finish to print all those values to the screen...
Half an hour, if we skip the print process and just do the calculations...

I think it's rather obvious where the bottleneck is, and it isn't in the nested FOR...NEXT loops.
Reply


Messages In This Thread
Why does my Loop end after 11 Loops? - by Dimster - 02-06-2023, 07:08 PM
RE: Why does my Loop end after 11 Loops? - by SMcNeill - 02-12-2023, 07:34 PM



Users browsing this thread: 1 Guest(s)