How to speed up floating-point to integer number conversion?
c, c++, floating-point, optimization, performance
Solution
Most of the other answers here just try to eliminate loop overhead.
Only deft_code's answer gets to the heart of what is likely the real problem -- that converting floating point to integers is shockingly expensive on an x86 processor. deft_code's solution is correct, though he gives no citation or explanation.
Here is the source of the trick, with some explanation and also versions specific to whether you want to round up, down, or toward zero: Know your FPU
Sorry to provide a link, but really anything written here, short of reproducing that excellent article, is not going to make things clear.
Problem
We're doing a great deal of floating-point to integer number conversions in our project. Basically, something like this ``` for(int i = 0; i < HUGE_NUMBER; i++) int_array[i] = float_array[i]; ``` The default C function which performs the conversion turns out to be quite time consuming. Is there any work around (maybe a hand tuned function) which can speed up the process a little bit? We don't care much about a precision.