The QBasic / QB64 Discussion Forum      Other Subforums, Links and Downloads
 
 


  << Previous Topic | Next Topic >>Return to Index  

Comparing Coding Methods (for speed)

February 26 2004 at 6:23 AM
  (Login Mac36)

 
The following results were obtained in pure DOS mode:

 10000 Tests of IF X3 >= x2 THEN IF X4 <= x1....etc.
 131  minus fraction: .6113888888888889 
 10000 Tests of IF X3 >= x2 AND X4 <= x1....etc.
 178  minus fraction: .3341666666666667 
 10000 Tests of IF (X3 <= x2) OR (X4 >= x1)....etc.
 178  minus fraction: .1115277777777778 

 100000 Tests of IF X3 >= x2 THEN IF X4 <= x1....etc.
 1314  minus fraction: .9279166666666666 
 100000 Tests of IF X3 >= x2 AND X4 <= x1....etc.
 1786  minus fraction: .355 
 100000 Tests of IF (X3 <= x2) OR (X4 >= x1)....etc.
 1788  minus fraction: .1845833333333333 

 200000 Tests of IF X3 >= x2 THEN IF X4 <= x1....etc.
 2628  minus fraction: .7984722222222222 
 200000 Tests of IF X3 >= x2 AND X4 <= x1....etc.
 3573  minus fraction: .5438888888888889 
 200000 Tests of IF (X3 <= x2) OR (X4 >= x1)....etc.
 3577  minus fraction: .2901388888888889 

using the following program

DECLARE SUB TestIt2 (Tests AS LONG)
DECLARE SUB TestIt3 (Tests AS LONG)
DECLARE SUB TestIt1 (Tests AS LONG)
DECLARE FUNCTION CurTime& ()
CLS
N$ = "0123456789"
OPEN "z.lst" FOR OUTPUT AS #1
PRINT #1, "Comparing Coding Methods"
LOCATE 10, 30: PRINT "Comparing Coding Methods"
LOCATE 15, 1
DO
PRINT : PRINT
  DO
    LINE INPUT "How many times to test? ", t$
    IF t$ = "" THEN CLOSE : SYSTEM
    IF LEN(t$) > 8 THEN t$ = "forget it"
    ok = 1
    FOR i = 1 TO LEN(t$)
      IF INSTR(N$, MID$(t$, i, 1)) = 0 THEN ok = 0
    NEXT i
  LOOP WHILE ok = 0
  PRINT #1, "": PRINT #1, ""
  CALL TestIt1(VAL(t$))
  CALL TestIt2(VAL(t$))
  CALL TestIt3(VAL(t$))
LOOP

FUNCTION CurTime& STATIC
DEF SEG = &H40
Lo& = PEEK(&H6C) + (256& * PEEK(&H6D))
Hi& = PEEK(&H6E) + (256& * PEEK(&H6F))
DEF SEG
CurTime& = (65536 * Hi&) + Lo&
END FUNCTION

SUB TestIt1 (Tests AS LONG)
PRINT Tests; "Tests of IF X3 >= x2 THEN IF X4 <= x1....etc."
PRINT #1, Tests; "Tests of IF X3 >= x2 THEN IF X4 <= x1....etc."
DIM i AS LONG
'=== Set Time to beginning just after new change
TempTime& = CurTime&
DO: OldTime& = CurTime&: LOOP WHILE OldTime& = TempTime&
'=== Do the operation
FOR i = 1 TO Tests
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 THEN IF X4 <= x1 THEN IF Y3 <= y2 THEN IF Y4 >= y1 THEN olap = 1
NEXT i
'=== See what time it is now
NewTime& = CurTime&
i = 0: WHILE NewTime& = CurTime&: i = i + 1: WEND
'=== Print results
PRINT NewTime& - OldTime&; " minus fraction:"; i / 7200
PRINT #1, NewTime& - OldTime&; " minus fraction:"; i / 7200
END SUB

SUB TestIt2 (Tests AS LONG)
PRINT Tests; "Tests of IF X3 >= x2 AND X4 <= x1....etc."
PRINT #1, Tests; "Tests of IF X3 >= x2 AND X4 <= x1....etc."
DIM i AS LONG
'=== Set Time to beginning just after new change
TempTime& = CurTime&
DO: OldTime& = CurTime&: LOOP WHILE OldTime& = TempTime&
'=== Do the operation
FOR i = 1 TO Tests
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
olap = 0
IF X3 >= x2 AND X4 <= x1 AND Y3 <= y2 AND Y4 >= y1 THEN olap = 1
NEXT i
'=== See what time it is now
NewTime& = CurTime&
i = 0: WHILE NewTime& = CurTime&: i = i + 1: WEND
'=== Print results
PRINT NewTime& - OldTime&; " minus fraction:"; i / 7200
PRINT #1, NewTime& - OldTime&; " minus fraction:"; i / 7200
END SUB

SUB TestIt3 (Tests AS LONG)
PRINT Tests; "Tests of IF (X3 <= x2) OR (X4 >= x1)....etc."
PRINT #1, Tests; "Tests of IF (X3 <= x2) OR (X4 >= x1)....etc."
DIM i AS LONG
'=== Set Time to beginning just after new change
TempTime& = CurTime&
DO: OldTime& = CurTime&: LOOP WHILE OldTime& = TempTime&
'=== Do the operation
FOR i = 1 TO Tests
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 4: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 10: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 15
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 7: Y4 = 16: y1 = 17
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 15
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
X3 = 5: x2 = 6: X4 = 11: x1 = 12: Y3 = 8: y2 = 9: Y4 = 16: y1 = 17
IF (X3 <= x2) OR (X4 >= x1) OR (Y3 >= y2) OR (Y4 <= y1) THEN olap = 0 ELSE olap = 1
NEXT i
'=== See what time it is now
NewTime& = CurTime&
i = 0: WHILE NewTime& = CurTime&: i = i + 1: WEND
'=== Print results
PRINT NewTime& - OldTime&; " minus fraction:"; i / 7200
PRINT #1, NewTime& - OldTime&; " minus fraction:"; i / 7200
END SUB

 
 Respond to this message   
AuthorReply
agamemnus
(no login)

it should be even faster with a DEFINT A-Z ;)..

March 17 2004, 8:32 PM 

The result is a bit confusing. Why not just say 130.4 instead of 131 minus ".611388888889"......? Could be misunderstood on first read. :)

 
 Respond to this message   

(Login neuro_)
R

meh, QB is a hack language

March 27 2004, 2:25 AM 

i consider QB to be the language of choice for one who needs to get something done in a hurry. If one is interested in making efficient code, C is the only 3rd generation language worth considering

 
 Respond to this message   
GreenMan
(no login)

I have always liked what Bjarne Stroustrup said

March 28 2004, 8:28 AM 

"A programming language serves two related purposes: it provides a vehicle for the programmer to specify actions to be executed, and it provides a set of concepts for the programmer to use when thinking about what can be done. The first aspect ideally requires a language that is 'close to the machine'....the second aspect ideally requires a language that is 'close to the problem to be solved'."

Everything is a trade-off. QB was designed to be closer to...certain types of problems...being addressed by certain types of programmers. Speed considerations don't count for much until you can produce a program that actually works.

 
 Respond to this message   
agamemnus
(no login)

hah, so he's endorsing BASIC...

March 28 2004, 12:24 PM 

Since compilers have become advanced enough to optimize minutae..

 
 Respond to this message   
agamemnus
(no login)

powerbasic.

March 28 2004, 12:23 PM 


 
 Respond to this message   
GreenMan
(no login)

Re: Comparing Coding Methods (for speed)

March 28 2004, 2:03 PM 

I don't think we'll have compilers that can really do that as well as a human until we have programs that can do things like write poetry and music, and hold up their end of an intelligent conversation. Optimizing minutae can only really be done with the benefit of a genuine understanding of what is important and what is not within the context of the problem -- an understanding that requires a broad perspective on the problem at a high level of abstraction. This is not something computer programs are particularly good at so far.

 
 Respond to this message   
Current Topic - Comparing Coding Methods (for speed)
  << Previous Topic | Next Topic >>Return to Index  
 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement  

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums