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.

Code Snippets / [DBC] Return Float vs Integer speed

Author
Message
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 18th Nov 2008 03:15 Edited at: 18th Nov 2008 22:32
[EDIT] Ooops! I didn't realize I put the whole message in a code box!

I came across something that I didn't expect. It seems that returning a float from a Function is faster than returning an Integer in DBC (maybe Pro too). I was always under the impression that integers are faster than floats in all cases. I posted this here because I assume there is a wider audience than on the DBC board and maybe this can be validated or found useful when creating functions when speed is of the essence.

Here are two versions of the same function that adds two integers. One returns an integer and the other returns a float(but still adding the integers). Over 1,000,000 iterations, the float return is faster by about 30 ms every time I run this:




Enjoy your day.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 18th Nov 2008 20:14
Well seeing as they're a different size I guess it kinda makes sense.

Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 18th Nov 2008 20:36
For me the integer version is faster.

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 18th Nov 2008 22:37
Mr. Tank, is that in Pro? I'd be curious if a few DBC users could just run the script a few times to see what was faster.

@Zotoaster
I think integers and floats are both 32 bit in DBC.

Enjoy your day.
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 18th Nov 2008 23:25
Yes it's pro.

I think DBC is even slower than DBP for text too. you could try either increasing the number of repetitions or move the print command until after the timer call. Perhaps that has some effect.

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 19th Nov 2008 06:47
DBP uses functions for practically all mathematical operations that don't involve pure integers, so it's not surprising that it handles floats slower than integers.

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 19th Nov 2008 08:24
Quote: "I came across something that I didn't expect. It seems that returning a float from a Function is faster than returning an Integer in DBC (maybe Pro too). "


This is what struck me as unusual.

Enjoy your day.
Kohaku
20
Years of Service
User Offline
Joined: 3rd May 2004
Location: The not very United Kingdom
Posted: 19th Nov 2008 11:04
Madrman in the IRC room pointed out that you're using integers in your float function; so the integer values have to be converted to a float.

You're basically giving the float function more work.

But floats will be slower anyway.


You are not alone.

Irojo
16
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 19th Nov 2008 17:14 Edited at: 19th Nov 2008 17:19
You fellows seem to be confused.

The float is significantly faster than the integer.

It says so in his post, and I recieved the same result.

Otherwise, you are using DBPro.

Quote: "move the print command until after the timer call."

Same results, because the time isn't being recorded when printing. See:




I download memory.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 19th Nov 2008 23:26
Quote: "Madrman in the IRC room pointed out that you're using integers in your float function; so the integer values have to be converted to a float.

You're basically giving the float function more work.

But floats will be slower anyway"


Just like Irojo said, I'm saying that DBC functions are returning floats FASTER than integers which is the opposite of what I and probably anyone would expect.

And I purposely placed the integers inside of both functions and in one converts to a float to bring the point home further. The Type conversion should even slow things down more but the return of the float even after converting from an integer is faster than returning an integer.

I don't know if anyone with Pro besides Mr. Tank actually ran the script to see what the results are, but I have a feeling Pro would be different because of actual type declarations. But for DBC users, it could make a big difference in their programs that use functions and return integer results.

Take a look at these distance formulas. One uses 100% integers the other uses 100% floats. The float function is faster by almost 200 milliseconds on my machine. The opposite of what I'd expect. This could be very good news for DBC users - unless I'm missing something that's staring me right in the face?!!?





Enjoy your day.
Kohaku
20
Years of Service
User Offline
Joined: 3rd May 2004
Location: The not very United Kingdom
Posted: 20th Nov 2008 10:47
Oh! DBC.

I remember using DBC. Playing music in Windows Media Player made it run faster. Most bizarre!

Good times though.


You are not alone.

Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 20th Nov 2008 12:12
Latch,

Quote: "Take a look at these distance formulas. One uses 100% integers the other uses 100% floats. The float function is faster by almost 200 milliseconds on my machine. The opposite of what I'd expect. This could be very good news for DBC users - unless I'm missing something that's staring me right in the face?!!?"


Back in the day, I ran a bunch of operation levels test on DB when tinkering with the idea of writing source level optimizer. So nothing comes as surprise anymore. Anyway, while i don't have DB installed on my current system (so i can't test here), but when testing against the timer() it's generally better to use smaller tests and average the results. The reason being is that during long tests such as 1,000,000 functions, DBVM or Windows can decided other background threads are more pressing and swap DB out/in. Which can distort the timings. By averaging across a smaller test we can see any real world bias that might exist in the functions/code we're testing...

PB sample. (to get the idea)



Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 20th Nov 2008 13:18
I understand your point and thank you! That makes a lot of sense but it's not so much the actual time difference as it seems in all circumstances the float return from a function in Classic is a bit faster. I can change the order (testing the float before the integer or integer before float), I ran the test as you suggest, but it ends up pretty much the same. I just found it interesting - and it could be a time saver so I thought I'd mention it and let those that would, test it for themselves.

Thanks everybody for your input!

Enjoy your day.
Irojo
16
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 20th Nov 2008 17:39
Thanks for your discovery Latch!


I download memory.

Login to post a reply

Server time is: 2024-11-22 07:13:45
Your offset time is: 2024-11-22 07:13:45