|programmer optimizations? firstname.lastname@example.org (Afsal C. Majeed) (1994-12-28)|
|Re: programmer optimizations? davidm@Rational.COM (1994-12-31)|
|Re: programmer optimizations? email@example.com.OZ.AU (1995-01-01)|
|Re: programmer optimizations? firstname.lastname@example.org (1995-01-02)|
|Re: programmer optimizations? jbuck@Synopsys.COM (1995-01-02)|
|Re: programmer optimizations? email@example.com (Erkki Ruohtula) (1995-01-11)|
|Re: programmer optimizations? firstname.lastname@example.org.OZ.AU (1995-01-05)|
|Re: programmer optimizations? email@example.com (1995-01-05)|
|Re: programmer optimizations? firstname.lastname@example.org (1995-01-12)|
|Re: programmer optimizations? email@example.com (Stefan Monnier) (1995-01-21)|
|Re: programmer optimizations? firstname.lastname@example.org (Dave Gillespie) (1995-01-11)|
|Re: programmer optimizations? email@example.com (David Toland) (1995-01-11)|
|Re: programmer optimizations? firstname.lastname@example.org (1995-01-23)|
|Re: programmer optimizations? email@example.com (1995-01-27)|
|[6 later articles]|
|From:||firstname.lastname@example.org (Bill Leonard)|
|Organization:||Harris Computer Systems, Ft. Lauderdale FL|
|Date:||Thu, 5 Jan 1995 16:54:47 GMT|
"Afsal C. Majeed" <email@example.com> writes:
> We are having small debate here as to the use of some of the
> code optimizations by the programmer himself.
> replacing (n / 4)
> by (n >> 2)
> replacing if ((i < 0) || (j < 0) || (k < 0))
> by if ((i | j | k) < 0)
> as you can well imagine, the list is endless
> Since this produces somewhat non portable code, the view is that
> such optimizations should be discouraged.
> [One reason that such optimizations are discouraged is that they're often
> wrong. Turning n/4 into n>>2 is only correct if n is unsigned.
Not only are such optimizations often wrong, they often are not optimal!
For instance, I remember a machine I worked on not too long ago in which
shift was exceptionally slow, whereas integer divide was pretty fair.
Turns out that dividing by small powers of 2 (e.g., 4) was better done by
shifting, but larger powers of 2 were not.
The best advice is to express the operation you actually want performed,
and let the compiler do the optimizing. Unfortunately, some programmers
write "n / 4" when what they actually meant was "shift n right by 2", and
vice versa. This can often result in sub-optimal code because the compiler
must maintain the semantics of the language, regardless of what you
*meant*, and because the compiler has less knowledge about the data than
the programmer (sometimes).
Harris Computer Systems Corporation
2101 W. Cypress Creek Road
Fort Lauderdale, FL 33309
Return to the
Search the comp.compilers archives again.