Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / What's the fastest 2D area check?

Author
Message
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 16th Oct 2009 00:37
I've been trying to streamline my area checking routines and I've come up with this


maybe changing mx*2 to mx+mx would be a bit quicker, I'm not really sure how to test this sort of thing.
Anyone come up with something better?

TGC Forum - converting error messages into sarcasm since 2002.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 16th Oct 2009 02:08
There are a few mutiplys and abs() which would slow things down a bit. The traditional boundary checks work pretty well. The first boundary check just gets a boolean result from binary comparisons, the second one is the standard if then AND boolean check, the third is your code put in function form.

The fastest, but not by much is the first function. The slowest seesm to be yours if I transcribed it into a function correctly. Another big slowdown is the assignment of mx=mousex() and my=mousey() inside the function. DBC's assignment operator '=' is really slow. I was able to squeeze a little more speed out of the first two functions by passing the value ofthe mouse a parameter instead of using an assignment operator.



Enjoy your day.
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 16th Oct 2009 04:28 Edited at: 16th Oct 2009 04:40
Is the IF statement faster or a () boolean check faster? "if a=1" VS "(a=1)".


[edit]
Okay I ran Latch's tests and to make a more accurate reading I placed a WAIT KEY right befor the timers go off as DB can return an inaccurate reading of speed at startup.

While looping at 10,000 it seems that Method2 is the fastest for me and the other two are about the same speed. When looping at 100,000 Method1 is the quickest Method3 took twice as long and Method2 is in between.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 16th Oct 2009 05:09
Quote: "Is the IF statement faster or a () boolean check faster? "if a=1" VS "(a=1)"."

The thing with an IF is calling the IF itself. It's an additional command that the interpreter has to decypher so I would think that an IF clause in an interpreted language (like DBC) would be a bit slower. In either case, I think '=' is always slow because the interpreter has to conclude based on context whether = is the assignment operator or a boolean test.



Enjoy your day.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 16th Oct 2009 18:58
Quote: "I think '=' is always slow because the interpreter has to conclude based on context whether = is the assignment operator or a boolean test."

That's always seemed stupid to me, why didn't they make assignments "==" or ":=" or something?

@Latch
I did the same test as you and my formula was embarassingly almost five times slower than the traditional method. I stored the mouse positions because I thought that would be quicker than getting the position twice.
I was trying to cut out those ANDs; is & faster then?

Thanks for looking at this, it's naturally something that is processed many times so I've always wanted to get a really fast routine.

TGC Forum - converting error messages into sarcasm since 2002.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 17th Oct 2009 00:36 Edited at: 20th Oct 2009 09:15
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



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:



So running the whole test and comparing 5 different types, the fastest ones are 4 and 5 which use variations of 1 and 2:



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.
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 20th Oct 2009 07:50
Quote: "When you use AND in an IF expression"


This is incorrect, the entire expression will be solved regardless.

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 20th Oct 2009 08:10 Edited at: 20th Oct 2009 09:05
Quote: "Quote: "When you use AND in an IF expression"

This is incorrect, the entire expression will be solved regardless."

That's strange, I thought DBC used short-circuit evaluation.

You're right! And here's a little proof that I used to test it. If it behaved as I thought, then the function sum(a,b) should not change the value of the global result(0). But it does so therefore both sides of the AND are being evaluated even when the first half is false.



Enjoy your day.
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 21st Oct 2009 07:29
Nah, neither DBC or DBpro support it.

Login to post a reply

Server time is: 2024-05-09 13:30:55
Your offset time is: 2024-05-09 13:30:55