Quote: " was trying to cut out those ANDs; is & faster then"
Not necessarily. They are 2 different operators.
old:
Quote: "When you use AND in an IF expression, the AND will stop any further comparisons as soon as the first condition ISN'T met. For example:
a=5
b=6
c=10
IF a > b AND c > b then do something
This statement won't even get past a > b and therefore end the conditional check early ( a boost in speed to not check the whole statment)."
corrected to:
AND is a logical comparision of both sides of the expression. If both sides evaluate to true, then it's true, if either to false then the statement is false. In some BASIC languages, only the first part of the expression is evaluated. If it's true, then the check goes on to the second expression. If it's false, it need not go on because the whole statment would be false. It's called short-circuit evaluation. This is useful for speed in comparing already assigned expressions. But it wouldn't be good if you had to create and evaluate a value returned by a function:
If a > b AND (important function that has to be run)
would never run the important function if AND in DBC used short-circuit evaluation.
However, & is a binary operator and will evaluate the whole expression because it returns a value based on bits being set or not:
If (a>b) & (c>b) then do something
This will evaluate (a>b)&(c>b) to be 0 at which point it will not execute the THEN. It has to do a little more work than the first statement.
But there is another snag in an interpreted BASIC. The whole line is first converted to a string then parsed out and changed into the proper byte code. My guess is that this is also an influence on the execution speed because the fastest version I've seen so far is just a variation of
function mouse_within2(x1,y1,x2,y2,mx,my)
if mx>=x1 and mx<=x2 and my>=y1 and my<=y2 then exitfunction 1
endfunction 0
If break the if then into 2 conditional tests, 1 for x and then 1 for y, the results are fastest when the mouse ISN'T in the bounds:
function mouse_within5(x1,y1,x2,y2,mx,my)
if mx>=x1 and mx<=x2
if my>=y1 and my<=y2 then exitfunction 1
endif
endfunction 0
So running the whole test and comparing 5 different types, the fastest ones are 4 and 5 which use variations of 1 and 2:
sync on
sync rate 60
x1=100
x2=400
y1=100
y2=300
position mouse screen width()-1,screen height()-1
text 0,0,"Looping 5 tests 4 times. Please wait...."
sync
wait 10
print
for lp=1 to 5
tim=timer()
for n=1 to 10000
a=mouse_within(x1,y1,x2,y2,mousex(),mousey())
next n
print "Version 1: ";timer()-tim
tim=timer()
for n=1 to 10000
a=mouse_within2(x1,y1,x2,y2,mousex(),mousey())
next n
print "Version 2: ";timer()-tim
tim=timer()
for n=1 to 10000
a=mouse_within3(x1,y1,x2,y2)
next n
print "Version 3: ";timer()-tim
tim=timer()
for n=1 to 10000
a=mouse_within4(x1,y1,x2,y2,mousex(),mousey())
next n
print "Version 4: ";timer()-tim
tim=timer()
for n=1 to 10000
a=mouse_within5(x1,y1,x2,y2,mousex(),mousey())
next n
print "Version 5: ";timer()-tim
print
sync
next lp
wait key
end
function mouse_within(x1,y1,x2,y2,mx,my)
result#=((mx>=x1)&(mx<=x2))&((my>=y1)&(my<y2))
endfunction result#
function mouse_within2(x1,y1,x2,y2,mx,my)
if mx>=x1 and mx<=x2 and my>=y1 and my<=y2 then exitfunction 1
endfunction 0
function mouse_within3(ax,ay,bx,by)
mx=mousex():my=mousey()
if abs(mx*2-ax-bx)<(bx-ax)
if abs(my*2-ay-by)<(by-ay) then exitfunction 1
endif
endfunction 0
function mouse_within4(x1,y1,x2,y2,mx,my)
`result=((mx-x1)|(x2-mx))|((my-y1)|(y2-my))
if ((mx-x1)|(x2-mx)) > 0
if ((my-y1)|(y2-my)) > 0 then exitfunction 1
endif
endfunction 0
function mouse_within5(x1,y1,x2,y2,mx,my)
if mx>=x1 and mx<=x2
if my>=y1 and my<=y2 then exitfunction 1
endif
endfunction 0
If we put the mouse inside the bounds, the results are similar to methods 1 and 2. So all in all, the 5th method seems to be the fastest because it's hands down faster than the others when the mouse is NOT in the box and as fast when the mouse IS IN the box. Maybe 4 tests would be even faster.
Enjoy your day.