0

Possible Duplicate:
Should I use multiplication or division?

Poking around in some (C++) 3D library I often see code like this:

float fInvLength = 1.0f / fLength;
rkAxis.x = x * fInvLength;
rkAxis.y = y * fInvLength;
rkAxis.z = z * fInvLength;

Is this really faster than this code:

rkAxis.x = x / fLength;
rkAxis.y = y / fLength;
rkAxis.z = z / fLength;

I'm primarily interested in C#/.NET performance but I don't mind some info about C++ as well.

Community
  • 1
  • 1
Sebastian Krysmanski
  • 8,114
  • 10
  • 49
  • 91
  • Altough the duplicate is primarily for Python. Maybe you find this also helpful: http://stackoverflow.com/questions/5053810/c-xna-multiplication-faster-than-division – Tim Schmelter Jul 26 '12 at 12:26
  • I remember implementing multiply and divide in asm on CPU that didn't have these computation unit. Divide did take more time to compute, but on modern hardware I really doubt that. Mayby in some very tight loop. – Lukasz Madon Jul 26 '12 at 12:27
  • I created a benchmark testing this code before it was closed. In my test (which does each section (as written) 1000000 times) the first method was faster (`00:00:00.0094078` versus `00:00:00.0222377`). As you can see, the difference is negligible; and would depend on machine and compiler. – Bob2Chiv Jul 26 '12 at 12:39

0 Answers0