No -- why would I do that?
Because it's helpful to know when your "optimization" isn't valid, otherwise
you're going to insert bugs into your code without knowing it.
Well, its always possible to out-*know* the compiler. You can, of
course, decide not to know, and assume the world is perfectly ISO C,
and that there is nothing to know outside of that. I am curious as to
whether or not you've ever encountered a compiler with a bug in it?
We all have, but I'd say with certainty that all of us have written far more
bugs in our own code than we've found in our compilers.
If your compiler is not smart enough to do strength reduction when it's
safe, you need a new compiler, period. This sort of "coding trick" makes
code harder to understand, introduces subtle bugs, and worst of all provides
zero performance benefit even when they're correct.
[...] It is in fact possible for a programmer to outguess
a compiler, at least some of the time, but I've never met or
even heard of anyone who could outguess multiple compilers on
multiple systems.
What? I'm not sure how to even respond to that.
I mean, I do that all the time; I've got webpages that *show* this kind
of thing. Anyone who does any serious cross-platform development
probably does this.
Anyone who does serious cross-platform compatibility writes as much of the
code in a portable fashion as possible and lets the compiler sort it out for
each platform. Any code which is not possible to make portable is
segregated by platform and tuned for the specific implementation in use, but
that doesn't matter since it's not _intended_ to be portable.
If the compiler doesn't do a good enough job optimizing the portable code
for each platform, you complain to the compiler vendor, not try to outsmart
it. Trying to outsmart the compiler costs far more in future code
maintenance than any possible benefit you could get from microoptimizations.
My real world includes doing exactly that, as just a standard activity.
Your claims about who you've met or heard of relative to this, seem
curious to say the least. I mean if you were CBF or Keith, then fine,
but you work for Sun. Your company makes a thing called "Solaris", and
it runs really well on multiple platforms. You think they were just
hoping the compilers were good enough?
Note that Sun uses their own compilers, so if they run into sections of code
that aren't optimized well, they can make the compiler better. That's a
far, far better use of coder time (which is an expense) than hacking user
code to cope with bad compilers because it's a force multiplier.
Well, I'm an old-school math-oriented based person. Knowing that
something is a power of 2 (like a multiplying factor) still fires
neurons in my head, even if it doesn't for you.
I'm sure all of us recognize that 8 == 2^3. But we also recognize we're in
the 21st century and the compilers we use do strength reduction, so we're
more concerned with expressing to the people who will maintain our code
later on what the intent was than we are with proving we're clever.
I also like putting things like this: /* ... */ in my code too; they take
lots of keystrokes for no gain at all.
If you think useful comments provide "no gain at all", that says more about
your competence as a coder than anything else in this entire thread.
If you're specifically referring to a comment containing only an ellipsis,
you're right, that's just a complete waste of time, and you should be
embarrassed to admit you do that. That's worse than no comments at all.
Oh ... well I don't know, maybe billions. But I suppose if n is always
less than a billion you must be right.
Given that it will provide zero speedup on any system you're likely to use,
any case where N is less than infinity proves him right. Not to mention all
the extra time you spent proving the change is safe that you'll need to
recoup. If you want to multiply by 8, just multiply by 8 and move on.
Profiling will tell you if that's not the best choice, but the decision
after that is _not_ whether to shift by 3 but which compiler to switch to.
S