# Comparing Coding Methods (for speed)

February 26 2004 at 6:23 AM

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
agamemnus

# 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

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

# 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

# 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

# powerbasic.

March 28 2004, 12:23 PM
 Respond to this message
GreenMan