Jesse said:
Yeah -- its called branch mis-predictions. Its a question of how
many of them you want to pay for. Its actually just a very simple
math problem.
You don't have a benchmark: That's a theory. [...]
I don't have a benchmark that I can share publicly. Remember this
example was used in some seriously proprietary code.
[...] The theory is that your compiler is converting your code
into inefficient instructions,
Well its deeper than that. A modern compiler can use a technique
called "unrolling" to speed up loops. Indeed many modern compilers do
that today. But this is premised on the notion that inner loops are
simple, short, and can improve instruction density as a result.
Leaving an if-conditional in the inside of your loops can prevent your
compiler from being able to unroll the loop, or from gaining much
benefit by doing it since it leaves extraneous instruction in the
execution path. Athlons are really fast at execution NOPs, but it
still has to decode them before it gets to other instructions.
and that set of code is where your program is spending a large
amount of time in that segment of code.
That isn't something I assume that this sort of code will do all the
time. Rather I assume that you've discovered some bottleneck in your
code, it looks like this, and you might want to do something about it.
Now, I don't know what processor you are running on, but with
the environment you are speaking of (Redmond/games), it seems
that it would probably be from the x86 line.
I'd like to point out that Nintendo has a base of operations in Redmond
WA as well.
[...] While processor architecture isn't one of my
strengths,
Ah. So if you don't know about it, then you should assume it isn't
there right?
This is condescending and adds nothing to the discussion.
No, questioning what it is, adds nothing to the discussion. This
technique is a form of hoisting. Most people do not question hoisting
as a valid method of optimization (besides some expectation that maybe
your compiler will do it for you.) But for some reason you are. How
do you expect me to respond to this?
Explain why I'm wrong: I'm saying that until you know how much
time your application is spending in a specific spot of code,
you _don't_ know if its worth obfuscating the code in order for
greater efficiency.
This is non-sequitor. After you figure out where your bottlenecks are
you have to have some approach to optimizing them (after considering
removing them or rearchitecting your code.) The technique I showed is
one such technique. So the argument that it might not be where a
bottleneck is, is not an argument against the effectiveness of a
technique which is only meant to be applied where bottlenecks are.
You don't even know _if_ obfuscating the code will lead to greater
efficiency: The compiler may optimize your code for you.
The reason I brought the technique up in the first place is because I
*know* compilers will *not* do that sort of optimization. This
particular kind of hoist is, in general, too hard for the compiler to
know how to do correctly, and beyond reasonable capabilities of proving
that it is correct (a C compiler in general, cannot prove that a
variable will remain constant unless it has some a priori knowledge
about the aliasing of all the variables used in the vicinity of the
code). I've looked at this with the best compilers in the industry --
its just too hard of a problem for them.
But you never showed that it *was* the bottleneck.
That's because whether or not it is a bottleneck will depend on what
circumstances it will be used. Its easy to show scenarios in which
that same code either is or is not a bottleneck. Its independent of
the technique. This comment can be applied to any optimization
technique.
If you improved your performance, it was by luck, not skill. [...]
This makes no sense whatsoever. I have never improved the performance
of code by luck -- I have no idea how that could ever happen. You've
engineered things by luck? That's an interesting trick I'd like to
see.
[...] You may have decreased the maintainability of the code for
no real performance gain.
Ok, I see -- you're saying that we're not supposed to know the
performance of the output of any code generated by a C compiler, and
yet you know *FOR SURE* that there is no real performance gain from
this technique?
What you seem to be doing is operating under the mistaken impression
that ordering instructions in certain ways will always lead to
faster code:
Really? Where did I say or even imply this? I encourage you to scour
10+ years of my USENET postings or any publication or webpage (you can
use iarchive to find all my old stuff going back to about 1997) for any
such suggestion by me.
[...] C does not state that a specific set of instructions will be
faster.
*You* brought content from my web page into this newsgroup. You
understand? In the twisted universe of comp.lang.c, you can't know the
performance, reliabliity, or behavior of anything. In my universe, I
deal with things more concretely. Get it?
If you got a problem with my conceptions of C or how C relates to the
real world, then why would you go out of your way to voluntarily bring
that content from way over on my webpage into this newsgroup?
PS: Your sig deliminator is screwed up: its dash-dash-space-return
Google is screwed up. My life does not revolve around reverse
engineering google.