Optimization Question - Floats or Ints?
Hello world! Yes, I know that was corny, please don't go away... :(
I was wondering when, in terms of optimization, it makes more sense to use floats over ints?
I am creating a game that runs calculations every second, using fixed update, for multiple status effects. In my current situation, I have a fullness stat that decreases with each calculation by a fixed amount. For simplicity, since calculations are run every second, it decreases by 1, and the fullness stat is initialized at the value in seconds I want it to last. That ends up being about 1000 seconds. Of course, I could get the same effect if I set fullness to 100, and decreased it by .1 every second, if I used float values instead of ints. Theoretically, with this kind of implementation, since these variables don't affect object positions and rotations, etc, if I found myself needing finer values I could just add digits to them in order to keep them as int variables (eg 10000 fullness decreasing by 10 each second, etc etc). This thought made me want to ask:
In terms of program speed from a calculation standpoint, how large do int values have to be before it is not at all justifiable in using them, and they should be converted into floats with decimal values?
Thanks for your answers!
run a for loop with your code a bazillion times and measure the difference in time on both. if you ask me, it's the wrong place to optimize speed at all. there are plenty other places where it really gets in your way, not floats or ints.
Thanks for the feedback! <3 What suggestions for documentation do you have, that covers the other places you are talking about?
There are many, many variables to performance.Especially between integer and floating point math. It varies strongly from processor to processor (even within the same family such as x86) because different processors have different pipeline lengths. Also, some operations are generally very simple (such as addition) and have an accelerated route through the processor, and others (such as division) take much, much longer.
The other big variable is where the data reside. If you only have a few values to add, then all of the data can reside in cache, where they can be quickly sent to the CPU. A very, very slow floating point operation that already has the data in cache will be many times faster than an integer operation where an integer needs to be copied from system memory.