Performance optimizations: float or int?
-
I am trying to increase the speed of several functions. In other languages one thing to do is using
int
instead offloat
as the variable type wherever possible.So, I suddenly found out, that the functions
clamp()
,min()
andmax()
always return their result as afloat
, even if the input parameters are all of the typeint
.Did anyone of you make the experience that it makes a difference in FUZE if
int
orfloat
is used for heavy calculations? -
Thanks @spikey we'll look into this.
-
I still cannot say, if it makes a difference at the end (maybe if my project is finished) but a vector always converts
int
intofloat
, too. So, currently no need to change anything I guess, until we come to the conclusion, that this would help with speed. Sooner or later I can tell.a = {1,2,3,4} print(a) update() sleep(10)
output:
{1.000000, 2.000000, 3.000000, 4.000000}
-
Funny you mention this, I only started using “float” very recently when I noticed others were doing it. If I wanted to use a variable within a function, I used to just write e.g lives = 0 before starting the function. I still don’t really know how this is better - just that my code now looks more like the smart guys’ code! I do know that ”var” doesn’t work in the same way.
-
-
var
just says: hey, I need to store something later, but I don't know yet, what it would be. (For me it feels like a soft-declaration). -
int lives
is the normal declaration, careful when you read from them, before writing something into it (can bevoid
, I think forarrays
). -
lives = 0
is the definition. But in FUZE its a combo, because it automatically does a declaration (here as anint
if you didn't before).
Just saw, you are a veteran coder, man: you did Tycoon Dizzy? What am I writing here... 🤭
-
-
Hahaha! Well thanks. Maybe don’t look at the actual coding and we’ll go with that :). I will keep using float as it seems to be working for me so far, and seems less like cheating, to say lives=0 when I don’t know what lives should equal just yet.
-
i think switch has HW FPU so doing integer maths or floating points does not show that big a off a difference as it would on systems using softfloat where fixed point math would help in that case (represent floats in integers and do calculation using the integer representations so that your not doing floating point maths but integer maths which is faster) and even if it does not it's got plenty of speed. It's not like your developping for some 200mhz device without floating point unit about 20 years ago i had such problems with gp2x but fixed point maths fixed that. But fuze will probably slow down more from doing lots of small (self written) function calls (depending on use case or when trying todo fixed point math) than doing floating point maths directly
edit: according to wikipedia switch uses tegra chip and ARM Cortex-A57 which has hw FPU
also you can easily time stuff using time() function and do little benchmarks, if you really want to investigate for example time 10000 or even 100000 time doing integer division and then do simiar using floating point division and compare times. Doing a single division will not give accurate results to compare as it completes way too fast to measure
T = Time() Do Some things you want to benchmark Printat(0,0, Time() - t)