Is it better to use float instead of mixing float and int in large calculation?
I have a fairly large formula that needs to be calculated about 300 times per second. Most of my variables are floats, but some variables are just parameters that go into this formula. In some cases, a float is not required. I could use int parameters. But I'm wondering if mixing int and float when computed will cause the system to create type types instead of saving performance.
a source to share
Be careful in assuming that int operations are always faster than floats. In isolation, they can be, but moving data between floating point and int registers is shocking on modern processors . Therefore, when your data is in a floating position, you must keep it in the float for further calculations.
a source to share
First of all, yes, the system will distinguish variables so that it can perform computations in a common base type. Second, this is not what you should be most worried about if you haven't compared your program and you know this is your bottleneck.
To clarify the first part, however, if you have
double a = 10.0;
int b = 5;
int c = 2;
and your formula
double d = a + (b / c);
most systems I know of will use integer math where they can, for division in this case, then switch to floating point math where appropriate. In short, the solution of numbers and floating point math is done at the operator level.
a source to share
Depends ... Is the correct result important to you?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int x = 5;
int y = 2;
float z = 12.3f;
float result = z + (x / y);
printf("%f\n", result);
result = z + ((float) x) / y;
printf("%f\n", result);
return EXIT_SUCCESS;
}
C:\Temp> t
14.300000
14.800000
Do what gives you the right results.
a source to share
I doubt there are good rules of thumb. It can be highly dependent on the processor, compiler, and the specific combinations of operations you are performing. Set up the timer to run before and after your calculation, and make various settings and see which one runs faster.
Basically converting from float to int would often slow you down because it requires additional instruction, but in practice the only way to tell is to measure. Looking at the assembly dumps of your various changes can help you get some good intuition, but even then the processor may pipelined or even reorder instructions in non-obvious ways.
Also, be sure to research gcc floating point optimization switches and make sure you use the best ones for them.
a source to share
I would say it depends on what is more important to you. It is better to use float, since by doing this you will not be entering the number of ints times (about 300 times per second;)).
If this is something that is used very often in your code, you may be concerned about it, otherwise there is no need for any changes. If you haven't heard or read the " Premature Optimization Bug" , now this might help you. The 20% code rule takes 80% of the performance.
a source to share