Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help

Basic optimising and profiling

Optimising

nocyclesBefore executing a long job, be sure you've optimised it. There are trade-offs between speed, program size, code readability and portability when you optimise the code, but you can sometimes obtain large benefits without unpleasant side-effects by

Note that in recent years CPU speeds have risen faster than memory speeds, so your bottlenecks may well be memory-related.

If you're using fortran or C/C++ look through the optimisation options of the compiler carefully! Use "man g++" to read about the C++ compiler, and "man g77" to read about the fortran compiler

Profiling

If the program still runs slower than you expect (or rather, hope!) then it may be useful to profile it to give you a better idea of where you should concentrate your energy.

Code coverage

It can be useful to know how many times each line of source code was run. If you put the following C++ code into foo.cc

  int main() {
    for (int i=0;i<5;i++)
      if (i>3)
        return 0;
    int j=7;
  }
and run
  g++ -fprofile-arcs -ftest-coverage  foo.cc
  ./a.out
  gcov foo.cc
the output will be
File 'foo.cc'
Lines executed:80.00% of 5
foo.cc:creating 'foo.cc.gcov'
and foo.cc.gcov will contain
        -:    0:Source:foo.cc
        -:    0:Graph:foo.gcno
        -:    0:Data:foo.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        1:    1:int main() {
        5:    2:  for (int i=0;i<5;i++)
        5:    3:    if (i>3)
        1:    4:      return 0;
    #####:    5:  int j=7;
        -:    6:}

showing that line 4 was reached once and line 5 never reached. Such information can be useful when deciding where to concentrate on optimising.

© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: Dember 2009