4

Possible Duplicate:
Floating point division vs floating point multiplication

recently, i have written a program that calculates how long it takes my computer to calculate real multiplications, divisions and additions.

for that, i have used the functions QueryPerformanceFrequency and QueryPerformanceCounter in order to get time intervals.

i have tested my program using 6,000,000 iterations : 6000000 multiplications, divisions and sums (with float variables), and get this results:

O.S = 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 the division is six times slower than multiplication, and addition practically equal than multiplication (~ 0.42 ns) ?

Community
  • 1
  • 1
  • **Q:** Is it possible that the division is six times slower than multiplication, and addition practically equal to multiplication? **A:** Yes. – David Heffernan Dec 30 '12 at 20:05

2 Answers2

0

Yes it is. If you have ever done division manually, then you know that it involves lots of sub-operations.

As for addition vs. multiplication being about the same speed: those have more to do with setting up the operands and storing the result than the actual computation. You might be able to get more difference between them by compiling using SSE instructions for addition and multiplication (on x86).

wallyk
  • 56,922
  • 16
  • 83
  • 148
0

With a modern CPU, multiplies are about the same speed as adds.

On Intel's Sandy Bridge, a multiply using SSE takes 4/3rd the time of an add. But with ILP taken into account, where multiple instructions can be processed at the same time, they actually take the same amount of time. On the same CPU, with ILP, single-precision divisions take 7-14 times as long!

Source: Agner Fog's instruction tables. Lots of very good reads at his site.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110