Monday, August 16, 2010

CODE OPTIMIZATION

CODE OPTIMIZATION

USER TECHNIQUES
***************


FORTRAN is used for heavy number crunching, and often decreasing the
running time of a program is desirable, however, it shouldn't be set
as a primary goal except on special cases.

While doing manual optimizations, it's a good idea to check the code
also from a numerical point of view. At least, avoid optimizations
that may lead to accuracy deterioration.


The need for profiling
----------------------
Since it's common that programs spend most of the execution time in
a few small parts of the code (usually some loops), it is recommended
to perform first "profiling", i.e. analysis of the relative execution
time spent in different parts of the program.

Profiling is also important when you apply optimizations, compilers
and computer hardware are complicated systems, and the results of
applying an optimization may be hard to guess.


Optimizations that can't be done automatically
----------------------------------------------
There are some optimizations you can do, which a compiler can't,
for example:

1) Using a fast algorithm
2) Using unformatted I/O
3) Using higher optimizations levels
4) Performing "aggressive" manual optimization
5) Writing in assembly language


Using a fast algorithm
----------------------
A good optimization is choosing a fast algorithm.
Other types of optimizations probably can't make linear sort (a N**2
algorithm) compete with heapsort (N*logN) for long sequences, or make
a Runge-Kutta integrator compete with a Rosenbruck integrator for
very stiff problems.

Make yourself familiar with basic good algorithms. See the chapter on
literature for references to some classic books on algorithmics.


Using unformatted I/O
---------------------
Unformatted I/O is often better because:

1) The CPU intensive translation from internal and external
representation is avoided.

2) The radix conversion and roundoff errors associated
with the translation are avoided, so the accuracy
of computations involving numbers written to, and
then read back from a file increases.

3) It is more efficient because unformatted data takes
less space, hence more data can be passed in each
I/O operation, and less (slow) operations are used.

4) The resulting data files are smaller.

The disadvantages of unformatted I/O are that data files are not
portable to other systems, and cannot be edited and studied directly
by a text editor such as EVE, Emacs or vi.

No comments:

Post a Comment