desktop said:
The problem is that I have that conditional in a loop that potentially
gets executed many times. So it would be nice if it was possible to
make the test once before the loop.
if (arg == 1) {
somevar = x++;
} else {
somevar = x--;
}
while (running) {
somevar;
}
Are you optimizing prematurely? How much improvement do you really
expect from eliminating a single 'if'?
Now, to get dir of the 'if' entirely you _could_ (does not mean that
you should) duplicate the 'while' and do
if (arg == 1) {
while (running) {
...
++x;
...
}
}
else {
while (running) {
...
--x;
...
}
}
Or even extract it into two separate functions. Once you do that,
you can actually compare the performance with the code that has
the 'if' in it. Unless the '...' in my example represent almost no
code, the difference is going to be single percent points. In my
book it's usually not worth the headache of maintaining duplicated
code.
V