What are the basic guidelines to write an optimised code.
Its not so easy to express them in a single post. I have some ideas
that you can read about here:
http://www.pobox.com/~qed/optimize.html
What points should one keep in mind for this ?
All coding/design changes, or constraints you put on your code
increases the pressure on it correctness and maintainability. One must
balance the cost of optimizing (such potentially changing the
operational limits, as the Y2K problem demonstrated, or just increasing
your bug rate or maintenance cost) versus the benefit (maybe your
program goes a little faster). But one of the main lessons of code
optimization is that you usually don't have to apply it to that much
code. By locating the hotspots or critical paths of your code
(commonly done with a runtime execution profiler), you can usually
isolate the process of code optimization to a very small fraction of
your code. So very often this cost can be quite low.
A lot of people posting here, are doing their best to discourage you
from attempting to optimize for some reason. Indeed if you feel you
are not able to balance multiple constraints on code development, and
need to make coding as simple a problem as possible, then optimization
may not be for you. I'm not sure why these other posters automatically
assume that you are not capable of putting more constraints on your
coding, but if they are right and you are a low skilled programmer,
then indeed, just stay away from optimization.
Is this purely architecture or complier specific?
In the most general sense, optimization is not limited to one or the
other. If you are really serious about optimization, you usually try
to think about all modes of optimization.
Are there any general techniques that are valid for
all architectures ?
Reducing algorithmic complexity (for example changing a O(n^2)
algorithm to a O(n*ln*(n)) one) in the face of large input data usually
applies to all architectures. Also removing redundant operations from
your inner loops usually is at worst no different, and often a benefit
to your program's performance.
There are also certain operations which are universally slow (like
division, modulo, trig functions, etc.) So using techniques such as
algebraic simplification to reduce their impact (known as "strength
reduction") usually leads improved performance (if these operations are
critical bottlenecks.)