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 Professional Discussion / ABS() function in DBPro

Author
Message
CSL
17
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: USA
Posted: 21st Feb 2014 18:53 Edited at: 21st Feb 2014 18:53
It is my understanding that the ABS() function from DBPro has a slight performance impact in applications.

If I were to write my own ABS() function, would this be the correct implementation?


CSL
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 21st Feb 2014 19:39
Yes, it seem correct. However, are you sure this would be faster than the inbuilt function? I'd find that quite surprising.

Also, this should be faster:



I haven't made any speedtests and don't know whether a few mathematical operations are slower than a single if evaluation. But it might be possible that this is even faster:



But still, I doubt any of those are faster than just calling DBPro's abs(). Have you checked, or is it just an assumption?

Le Verdier
14
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 21st Feb 2014 19:44 Edited at: 21st Feb 2014 19:54
Edit:
Lost the race..
Said the same thing...

Are you using integer??
You can also "inline" the function

i.e paste
if num < 0 then num = -num
directly

ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 21st Feb 2014 20:02 Edited at: 21st Feb 2014 20:04
Amazing...



Indeed the DBP function is the slowest of all the tested ones by far. I get these results:
3475 ms for abs()
193 ms for myabs1()
353 ms for myabs2()
119 ms for Le Verdier's inline method (at least his function has won the race )
179 ms for the arithmetic inline method


However - unless you're working with a lot of data (such as image processing for instance), this will still most certainly not make a notable difference. So just use whatever feels most comfortable coding-wise.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Feb 2014 20:35 Edited at: 21st Feb 2014 20:36
You sure you typed that DBP value correctly? 3475? Because for me, DBP's function is the fastest.

Here's my results from your test program:

54 -> abs()
151 -> myabs1()
271 -> myabs2()
68 -> le verdier's inline
96 -> arithmitic inline
174 -> my method





I tried to just simply do a bitmask; return the number with the 32 bit blocked.


But that didn't work. I thought that bit held the sign of the number, but I tried printing out the numbers in binary in DBP and any negative number had inverted the 1's and 0s. So -1 looked like: 11111111111111111111111111111110
but I thought it was suppose to be this: 10000000000000000000000000000001

Jeff Miller
21
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: New Jersey, USA
Posted: 21st Feb 2014 20:37
Load IanM's Matrix Utilities 7. His ABS() function is quite fast.
Le Verdier
14
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 21st Feb 2014 20:44 Edited at: 21st Feb 2014 20:45
M1u have an int version. It 's why Phelax got better result


ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 21st Feb 2014 21:11
@Phaelax, about the sign bit.

I think your method would work just like that with floats. However, we can do something similar with integers:



That seems to work. However, it's roughly as fast as the two functions from my code above, the inline versions are still faster. And yes, abs() does take that much time here, I double checked. Maybe it's really the fact that it doesn't differ between integers and floats, like Le Verdier said.

CSL
17
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: USA
Posted: 21st Feb 2014 22:15
A while back I did ran a similar benchmark to compare using the built-in abs() and the inline method, however I wasn't sure if there was something else behind abs() that my weak math foundation had not caught. The inline method seemed too simple.

Also, I noticed some odd behavior on programs that used randomize with timer() as a seed. With abs(), my sprites seemed to take a more random direction, without abs() the sprites seemed to take the same direction most of the time. This was months ago, I'm re-visiting an older program that uses abs() extensively and my mind started wondering as it usually does, I could be wrong (I don't have DBPro on the computer I'm using).

I'll take a look at all the options presented and see if there's a noticeable performance boost in my program and no strange behavior.

CSL
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Feb 2014 01:29
Quote: "You sure you typed that DBP value correctly? 3475? Because for me, DBP's function is the fastest."


Same here.

Quote: "Load IanM's Matrix Utilities 7. His ABS() function is quite fast."


I have IanM's utilities installed. How can we tell which version is being used, i.e. DBPro or Matrix1? I think IanM said his utilities take precedence. Not sure though.

I vaguely recall this same issue being discussed on another thread recently.



Powered by Free Banners
Le Verdier
14
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 22nd Feb 2014 02:07
If float in input -> dbp
If int -> m1u
There is no name collision because the "decorated" name (name+input types)(abs@zzzapapau.. Or something like this) in the Dll is taken into account. Same principle that functions with different number of parameters.

Jeff Miller
21
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: New Jersey, USA
Posted: 22nd Feb 2014 13:40
The thread started with a question involving code proposed as a function of an integer value. So IanM's ABS() is the fastest for that purpose. I get results like Phaelax.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 22nd Feb 2014 17:53
Same here.



Powered by Free Banners
CSL
17
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: USA
Posted: 24th Feb 2014 20:41 Edited at: 24th Feb 2014 22:03
Hmm, I've been always reluctant to add external "unofficial" plugins because of potential conflicts, but I think at this point it makes sense to do it, since DBPro hasn't been updated in quite a while. I'll give IanM's Matrix Utils a try. I noticed it also improves other numeric and string functions as well.

Is this the link to the latest version?

https://forumfiles.thegamecreators.com/download/2355306

CSL
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 24th Feb 2014 23:02 Edited at: 24th Feb 2014 23:03
The M1Util DLL's *should* be official. They should come with the DBPro installer they're that good/useful!)

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Feb 2014 23:21
I agree.



Powered by Free Banners
CSL
17
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: USA
Posted: 25th Feb 2014 02:42
Results after installing the M1Util DLL's:

57 -> abs()
149 -> myabs1()
270 -> myabs2()
67 -> le verdier's inline
94 -> arithmetic inline

The new abs() is significantly faster.

CSL
BMacZero
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 27th Feb 2014 20:15
@Phaelax

Two's complement

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 27th Feb 2014 23:51
The standard ABS function accepts a float, so when you try to get the absolute value of an integer, DBPro first converts your integer to a float, then executes the function, then depending on your code, converts the resulting float back to an integer - it's those conversions that take up the time.

My version of the function accepts an integer directly, removing those conversions.

When you compile with DBPro, it tries to find a best match - in this case, when you pass in an integer it calls mine, and if you pass in a float it calls the standard one.

Login to post a reply

Server time is: 2026-07-06 22:18:49
Your offset time is: 2026-07-06 22:18:49