speed of floating point multiplication vs. division
Long time ago I heard that the speed of floating point multiplication is much faster than division. Is it still the case today? (On a not very powerful netbook) Thanks!
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
Re: speed of floating point multiplication vs. division
What I learned when I was doing CPU architecture research is that not only is division costly (about 40 cycles), vs a multiplication (about 10 cycles), divisions are not "pipe-able".
What this means is that once you order your processor to do the division, you'll have to wait until it is finished before starting a new division. Multiplications, on the other hand, you can request a new multiplication on every cycle. You'll still have to wait the full 10 cycles for each to finish. So for example, if you need to do 10 multiplications, you can spend the first 10 cycles requesting the multiplications, and the during the next ten cyces, you'll get 1 result per cycle.
All of this is very theoretical of course. The rule of thumb is "division is kind of expensive", but quite frankly, as always, these kinds of little optizations are the last thingg you should worry about. I've NEVER seen an algorithm where it really made a change.
If you must divide, then you must divide...
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
Re: speed of floating point multiplication vs. division
Originally Posted by S_M_A
A modern compiler will replace (if it's possible) a multiplication/division by a 2 base with shifts. An example from a MSVC2005 debug(!) build[code]
Even a non-modern compiler will do this.
Back in the world of 16-bit DOS systems (Visual C++ 1.x, Turbo C) , there was no guarantee that the machine had a floating point processor installed, so why introduce emulated FP when not necessary.
I know that for Turbo C, Borland made sure that the underlying code did not use any emulated floating point unless it was necessary (and sometimes you had to force the linker to use the floating point emulation).
Re: speed of floating point multiplication vs. division
Yes I remember that as well but for integer operations I doubt that the co-processor was in question.
The reason I wrote modern was merely that I neither remember nor can test how such an old compiler behaves.
Edit: I just remembered that some years ago I cleaned up some old 16 bit stuff and before throwing it nostalgy made me make a virtual Win3.11 machine. Here's a screen dump
Last edited by S_M_A; June 3rd, 2012 at 03:52 PM.
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
Re: speed of floating point multiplication vs. division
Originally Posted by S_M_A
The reason I wrote modern was merely that I neither remember nor can test how such an old compiler behaves.
Mainly added for completeness: I think I can assure that MS C 6.0 (most probably early 90s, running under DOS) already did that "prefer SHL/SAR/SHR over MUL/IMUL/DIV/IDIV when possible" optimization. Probably even some earlier compilers did that as well, but I'm not definitely sure about that. IIRC they even did replace multiplications by constants with few one-bits with SHL/ADD combinations.
BTW, haven't seen anything like your Win 3.11 screen shot for a really long time...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Re: speed of floating point multiplication vs. division
Originally Posted by Eri523
BTW, haven't seen anything like your Win 3.11 screen shot for a really long time...
And we shall all be grateful for what has replaced it. Even though I remember that back in those days we all were very exited. The change from running codeview in a DOS screen to running the debugger in a proper window was great. Today that old environment felt very very akward to handle though...
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
Re: speed of floating point multiplication vs. division
Originally Posted by S_M_A
Edit: I just remembered that some years ago I cleaned up some old 16 bit stuff and before throwing it nostalgy made me make a virtual Win3.11 machine. Here's a screen dump
Well S_M_A, that's a lot of dedication just to see what 16-bit Visual C++ handled divisions and multiplications.
Re: speed of floating point multiplication vs. division
Originally Posted by acppdummy
Thank you both! How about integer multiplication vs. shift? For example <<2 vs. *4 ? Thanks!
shift is ALWAYS faster. However, you shouldn't need to worry about that. The compiler is smart enough to know that and fix it for you. In fact, shift is so much faster that to multiply by six (which is not shiftable)
Code:
x = (y << 2) + (y << 1);
is faster than
Code:
x = y * 6;
But again, your compiler should do that for you. And I'm not sure this is still the case, I know it was the case on 486 and before, but processors have changed a lot. No matter how much processors change though, a bit shift will always be faster than a multiplication, its probably actually the fastest instruction their is. Of course, it's only useful for integers, so it doesn't affect the original query.
Like everyone else said though, the pipeline and the cache have a much bigger impact.
Re: speed of floating point multiplication vs. division
Originally Posted by acppdummy
Does the speed depend on how many bits are shifted? For example would >>16 take longer than >>1 ? Just curious. Thanks!
Not on any architecture I now of.
It may depend on the size of the object being shifted though, in particular, for when the long long type that doesn't fit inside a register (eg, it is emulated). Though in this case, to be fair, the instruction doesn't actually exist, the assembly code is hand written by the compiler writers.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.