Re: The performance of all kinds of C operations
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Sat, 17 Jun 2006 21:51:02 GMT
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.
.
- Follow-Ups:
- Re: The performance of all kinds of C operations
- From: websnarf
- Re: The performance of all kinds of C operations
- From: jacob navia
- Re: The performance of all kinds of C operations
- References:
- Re: The performance of all kinds of C operations
- From: Ronald Bruck
- Re: The performance of all kinds of C operations
- From: Tim Prince
- Re: The performance of all kinds of C operations
- From: Roberto Waltman
- Re: The performance of all kinds of C operations
- From: spibou
- Re: The performance of all kinds of C operations
- From: websnarf
- Re: The performance of all kinds of C operations
- From: Eric Sosman
- Re: The performance of all kinds of C operations
- From: websnarf
- Re: The performance of all kinds of C operations
- From: Nick Keighley
- Re: The performance of all kinds of C operations
- From: websnarf
- Re: The performance of all kinds of C operations
- From: Nick Keighley
- Re: The performance of all kinds of C operations
- From: websnarf
- Re: The performance of all kinds of C operations
- Prev by Date: Re: knowing exact string array length ?
- Next by Date: Re: Educated guesses for efficiency?
- Previous by thread: Re: The performance of all kinds of C operations
- Next by thread: Re: The performance of all kinds of C operations
- Index(es):
Relevant Pages
|