Re: The performance of all kinds of C operations



websnarf@xxxxxxxxx writes:
Nick Keighley wrote:
websnarf@xxxxxxxxx wrote:
Nick Keighley wrote:
websnarf@xxxxxxxxx wrote:
Eric Sosman wrote:
websnarf@xxxxxxxxx wrote:
spibou@xxxxxxxxx wrote:
I didn't read your whole page but had a look at the table in the
section "Strictly for beginners". Can you explain why would
"x = y << 3" be faster than "x = y * 8" ? [...]

I was using a compiler in the '80s that was smart enough to replace * 8
with << 3. Stuff like this in the source just obscures the intent.

Explain how it obscures the intent.

obviously we are not on the same wavelength. If I want to multiply by 8
I write *8. Why would I do anything else? (unless measurement showed
<<3 was faster *and* this line of code needed to be faster).

If shifting by 3 is the *same* as multiplying by 8, then how is
shifting by 8 "doing something else" other than multiplying by 8? Tell
me, why do you think C includes a << operation?

For bitwise shifting, of course. Why do you think C includes a "*"
operation? Even if they happen to be mathematically equivalent in
certain cases, they're *conceptually* very different.

If I want something to be 8 times as big, I'll multiply it by 8.
(Tomorrow I might decide I want it to be 9 times as big.)

If I want to shift the bits of an object left by 3 positions I'll use
"<< 3". If shifting is really what I want to do here, I might decide
tomorrow that I want to shift by 4 bits; it's unlikely I'll decide I
want to multiply by 9.

I'll use the multiplication if it expresses what I want to do. If a
shift happens to be more efficient, I'll trust the compile to generate
a shift instruction -- or not to if it can't prove that it's really
equivalent, or if a multiplication is just as fast as a shift.

What if y is negative? (Answer: undefined behavior.) What if y is
floating-point? (Answer: you can't apply "<<" to floating-point
values.)

Finally, even assuming that y is unsigned or positive, what happens if
I replace this:
x = y * 8 + z;
by this:
x = y << 3 + z;
? The answer depends on the relative precedence of the "*", "+", and
"<<" operators. I don't know about you, but I had to look it up; even
if you have all the operator precedences memorized, the next person
who reads your code probably hasn't.

Now, please explain how
x = y << 3;
is *better* than
x = y * 8;
particularly for beginners. (Answer: it isn't.)

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: The performance of all kinds of C operations
    ... table in the section "Strictly for beginners". ... I was using a compiler in the '80s that was smart enough to ... If shifting by 3 is the *same* as multiplying by 8, ... present an future platforms on which my code might run. ...
    (comp.lang.c)
  • Re: The performance of all kinds of C operations
    ... shifting by 8 "doing something else" other than multiplying by 8? ... If I want to shift the bits of an object left by 3 positions I'll use ... Modern processors are highly complex beasts, ... Those "optimization" are from the days ...
    (comp.lang.c)
  • Re: The performance of all kinds of C operations
    ... section "Strictly for beginners". ... I was using a compiler in the '80s that was smart enough to replace * 8 ... If shifting by 3 is the *same* as multiplying by 8, ... If I want to shift the bits of an object left by 3 positions I'll use ...
    (comp.lang.c)
  • Re: The performance of all kinds of C operations
    ... I was using a compiler in the '80s that was smart enough to replace * 8 ... If shifting by 3 is the *same* as multiplying by 8, ... To left shift? ... Unnecessary optimisation is even worse than premature optimisation. ...
    (comp.lang.c)
  • Re: TigerSHARC efficiency : multiply or shift?
    ... I'm used to coding for desktop apps, and whilst my C code runs fine I'm not sure about how long various operations take in terms of clock cycles on this T'SHARC. ... Looking through the various manuals, I note it has a nice bit barrel and also a multiplication unit. ... I tend to turn multiplications into bit shift in my code, but I realise now that I don't know which is more efficient in clock cycles on a T'SHARC, multiplying or bit shifting. ...
    (comp.dsp)