Qbasicnews.com
October 01, 2020, 04:13:52 PM
 Pages: 1 [2]
 Author Topic: A musical note from QuickBASIC with XP?  (Read 7045 times)
Ralph
Ancient Guru

Posts: 544

 « Reply #15 on: January 04, 2007, 03:58:41 PM »

Dr and Skyler:

Code:
'LoopTime times various loops in QuickBASIC

CLS

n# = 10000000
PRINT
A\$ = "Comparison of average times in 10 loops of three loop-times in QuickBASIC 4.5"
PRINT SPC(40 - LEN(A\$) / 2); A\$
PRINT
PRINT "                 T I M E    I N    S E C O N D S "
PRINT "               ----------------------------------"
PRINT "  Iter. No.     FOR/NEXT    DO/LOOP    WHILE/WEND"
PRINT "  ---------    ----------  ----------  ----------"

FOR J = 1 TO 10
LOCATE 7 + J, 8: PRINT J

'FOR/NEXT
t0 = TIMER
FOR i = 1 TO n#
xx = xx + 1
NEXT i
tf = TIMER
tfn = tfn + tf - t0
LOCATE 7 + J, 16: PRINT tfn / J

'DO/LOOP
i = 0
t0 = TIMER
DO WHILE i < n#
i = i + 1
LOOP
tf = TIMER
tdl = tdl + tf - t0
LOCATE 7 + J, 16 + 12: PRINT tdl / J

'WHILE/WEND
i = 0
t0 = TIMER
WHILE i < n#
i = i + 1
WEND
tf = TIMER
tww = tww + tf - t0
LOCATE 7 + J, 16 + 24: PRINT tww / J

NEXT J

These are the answers for the 10th iteration:
FOR/NEXT = 5.829 secs
DO/LOOP = 5.930 secs
WHILE/WEND = 5.966 secs

As can be seen, all took the same time, 6 seconds, practically speaking.

If I take out the unnecessary line, "xx = xx + 1", that I stuck in the FOR/NEXT loop to "equalize" it to the other two loops, its time drops, dramatically, to around 3 seconds.  It would seem that each line takes 3 seconds to execute.

 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Skyler
Ancient Guru

Posts: 564

 « Reply #16 on: January 04, 2007, 05:02:18 PM »

So... use FOR-NEXT?
 Logged

In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Dr_Davenstein
Na_th_an

Posts: 2052

 « Reply #17 on: January 04, 2007, 09:44:13 PM »

By adding the xx=xx+1 in the For...Next loop, you're making it work twice as hard as the others. It's already counting, where the others aren't, ya know?
 Logged
Skyler
Ancient Guru

Posts: 564

 « Reply #18 on: January 04, 2007, 09:55:36 PM »

Comparing the for-next to the while-wend and do-loop isn't really fair. They're designed for different things. It's like comparing apples to oranges, by painting the apples orange.
 Logged

In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Dr_Davenstein
Na_th_an

Posts: 2052

 « Reply #19 on: January 04, 2007, 10:09:22 PM »

Quote from: "Skyler"
Comparing the for-next to the while-wend and do-loop isn't really fair. They're designed for different things. It's like comparing apples to oranges, by painting the apples orange.

True, but by all means, use For...Next whenever possible.

For instance...

If you wish to loop through each element of an array:

Code:
For i = Lbound(Array) To Ubound(Array)
Print Array(i)
Next

For...Next will always win in such case.

And for God's sake, don't paint your apples orange!!!  :rotfl:
 Logged
Ralph
Ancient Guru

Posts: 544

 « Reply #20 on: January 04, 2007, 10:17:31 PM »

Quote from: "Dr_Davenstein"
By adding the xx=xx+1 in the For...Next loop, you're making it work twice as hard as the others. It's already counting, where the others aren't, ya know?

Just as I figured!  Twice as hard, because the loop has to loop through two lines, instead of one, since the first line is passed through only once, the first time around.  And, the results bear this out, giving 6 seconds for the two lines, and 3 seconds for one.

Of course, I used n# =10,000,000, in order to get some meaningful time.

From all the above, I see that any lines that can be deleted or compressed will have a measureable time-reduction on a very large number of iterations, which is what I think the Dr's reason for measuring loop-times was.

So, it all boils down to, "reduce the number of lines of code as much as possible. in any many-times iterative loop".  Reducing the time in that manner, rather than the type of loop used, seems to be the significant conclusion, so far.
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Dr_Davenstein
Na_th_an

Posts: 2052

 « Reply #21 on: January 04, 2007, 10:40:04 PM »

Exactly. Rel and I were attemptig to make a meaningful 3D engine with QB4.5, before FreeBASIC was released. Thankfully, we didn't waste a bunch of time. Even with Assembler, QB is much too slow to produce a fast 3D engine. It's ok for models with a very low amount of vertices, but when you try to add shadow extrusion, bump mapping, etc... it's just too slow.
 Logged
Ralph
Ancient Guru

Posts: 544

 « Reply #22 on: January 05, 2007, 12:00:28 AM »

wow!!!  I tried my program in FB, and, even using n# = 100000000,
and, instead of using one 10-cycle loop, I tried two 100000000-cycle loops, and I was just barely getting some decimal places for the slowest-running WHILE/WEND loop!!!  FB is faster then Superman!
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Skyler
Ancient Guru

Posts: 564

 « Reply #23 on: January 05, 2007, 10:57:02 AM »

Not faster than an Intrepid-class ship, though.
 Logged

In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Ralph
Ancient Guru

Posts: 544

 « Reply #24 on: January 05, 2007, 08:41:05 PM »

Antoni:

After I ran my original program in FB, without modifications, and thought it was super, super, super fast, I got to thinking, "Maybe I have to dimension my variables first, otherwise, they are not recognized, and, the program executes extremely fast, jumping all the loops!"  So, I modified the original program by dimensioning the variables first, and multiplying the constant, n#, by 150.  That seems to be the approximate speed multiplier using FB versus QB.

These are the FB answers for version 0.14:
FOR/TO 3.132, FOR/TO+ 3.202, DO/LOOP 5.823, WHILE/WEND 5.794
Seems the FOR/TO loops ran almost at the same speed, and almos twice as fast as the other loops.

Here is my FB code:
Code:

'LoopTimeFB times various loops in FreeBasic, version 0.14

CLS
dim i as long, J as long, k as long, t as single, t0 as single, t1 as single
dim  t2 as single, t3 as single, t4 as single
PRINT
A\$ = "Comparison of average times in 10 loops of three loop-times in FreeBasic"
PRINT SPC(40 - LEN(A\$) / 2); A\$
PRINT
A\$ = "Second LOOP, FOR/NEXT+, has the unnecessary line, i = i + 1"
PRINT SPC(40 - LEN(A\$) / 2); A\$
PRINT
PRINT " Iteration            T I M E      I N      S E C O N D S            "
PRINT "           ----------------------------------------------------------"
PRINT "    No.      FOR/NEXT       FOR/NEXT+       DO/LOOP      WHILE/WEND  "
PRINT " --------- -------------  -------------  -------------  -------------"

n# = 10000000*150

FOR J = 1 TO 10
LOCATE 9 + J, 6: PRINT J

'FOR/NEXT
i = 0
t0 = TIMER
FOR k = 1 TO n#
NEXT k
t = TIMER
t1 = t1 +t - t0
LOCATE 9 + J, 4 + 12 - 5: PRINT t1 / J

'FOR/NEXT+
i = 0
t0 = TIMER
FOR k = 1 TO n#
i = i + 1
NEXT k
t = TIMER
t2 = t2 + t - t0
LOCATE 9 + J, 4 + 24-2: PRINT t2 / J

'DO/LOOP
i = 0
t0 = TIMER
DO WHILE i < n#
i = i + 1
LOOP
t = TIMER
t3 = t3 + t - t0
LOCATE 9 + J, 4 + 36 + 1: PRINT t3 / J

'WHILE/WEND
i = 0
t0 = TIMER
WHILE i < n#
i = i + 1
WEND
t = TIMER
t4 = t4 + t - t0
LOCATE 9 + J, 4 + 48+4: PRINT t4 / J

NEXT J

for i = 1 to 2:i=0:next i
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
 Pages: 1 [2]