C ++ floating point operations

Possible duplicate:
Floating point division versus floating point multiplication

Recently, I wrote a program that calculates how much time my computer takes to calculate real multiplications, divisions and additions.

for this, I used the functions QueryPerformanceFrequency and QueryPerformanceCounter to get time intervals.

I tested my program using 6,000,000 iterations: 6,000,000 multiplications, divisions and sums (with floating variables) and get the following results:

OS = Windows Vista (TM) Home Premium, 32-bit (Service Pack 2) Processor = Intel Core (TM)2 Quad CPU Q8200 Processor Freq = 2.33 GHz Compiler = Visual C++ Express Edition nΒΊ iterations time in micro seconds 6000000 x real mult + assignment -> 15685.024214 us 6000000 x real div + assignment -> 51737.441490 us 6000000 x real sum + assignment -> 15448.471803 us 6000000 x real assignment -> 12987.614348 us nΒΊ iterations time in micro seconds 6000000 x real mults -> 2697.409866 us 6000000 x real divs -> 38749.827143 us 6000000 x real sums -> 2460.857455 us 1 Iteration time in nano seconds real mult -> 0.449568 ns real div -> 6.458305 ns real sum -> 0.410143 ns 

Is it possible that division is six times slower than multiplication, and addition is almost equal to multiplication (~ 0.42 ns)?

+4
source share
2 answers

Yes it is. If you've ever done manual separation, you know that it includes many sub-operations.

Both for addition and for multiplication, about the same speed: they have more in common with setting operands and saving the result than the actual calculation. You can get more differences between them by compiling with SSE instructions for adding and multiplying (by x86).

0
source

When using a modern processor, multiplication occurs at about the same speed as when adding.

On Intel Sandy Bridge, SSE multiplication takes 4/3 of the addition time. But given ILP, when multiple instructions can be processed simultaneously, they actually take the same amount of time. On one processor, with ILP, divisions with one precision take 7-14 times more!

Source: Agner Fog Datasheets . He reads a lot on his site.

0
source

All Articles