[GRASS5] Coding optimizations

Glynn Clements glynn at gclements.plus.com
Tue Feb 15 07:53:08 EST 2005


Brad Douglas wrote:

> I have a question regarding coding policy.  Is it acceptable to add
> basic optimizations such as bit shifting for exponential
> multiplication/division and hard-coding values like sqrt(2.) to an
> arbitrary precision?
> 
> Generally, these are things the compiler should be capable of, but is
> sometimes not the case.

In normal algebraic expressions, I would write x*2 rather than x<<1 on
the grounds of legibility. I would only use bit-shifting if it more
accurately reflected the nature of the code, not as an optimisation.

OTOH, I would normally define sqrt(2.) as a constant. Writing SQRT2
isn't really any less legible than sqrt(2.), might be substantially
more efficient (particularly if the architecture doesn't have a
square-root instruction), and doesn't require linking against the math
library.

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list