Re: Habitual optimization
hattons@globalsymmetry.com (Steven T. Hatton) wrote (abridged):
My question is whether any clock cycles might be saved by
moving some of the arithmetic out of the loops?
Probably. The drawback to pre-calculating and storing the value in a
variable is that it uses more memory. (Or rather, the same memory for
longer.) You may find that all the other code in the loop causes the
variable to be spilled out of hardware caches. It's conceivable that:
x = rect.left() + (i * (rect.width() - 1) / settings.numXTicks);
be quicker to execute than:
x += dTick;
if dTick requires a memory read and the other expression doesn't. It's
unlikely, but to be sure you'd need to measure.
Mind you, the difference will probably be insignificant anyway, compared
to the work being done elsewhere in the loop. Pure arithmetic expressions
with no tests or branches, and all functions inlined, tend to be jolly
quick on modern CPUs.
I also realize my arithmetic modifications might change the behavior of
the code due to the evaluation order of the multiplicative operations.
Indeed. In particular, you'll probably get rounding errors that accumulate
as the loop progresses. For example, if settings.numXTicks is 20 and
rect.width() is 155, then dTick is 7 and dTick*20 is 140, which is wrong
by 14 units. That is probably a significant bug.
-- Dave Harris, Nottingham, UK.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]