|
|
|||
![]() |
Department of Engineering |
| University of Cambridge > Engineering Department > computing help |
Before 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
The g++ compiler has dozens of optimisation options that can be selected individually. Using the -O3 option switches on the most aggresive methods. Note however that optimisation can make a program fail that was (fortuitously) working. For example, using the +Onoinitcheck will mean that variables won't be initialised to 0 automatically unless the C++ specification says so.
gfortran -pg -ocrunch crunch.f90 ./crunch gprof crunchThis produces a lot of output. The most important information is at the start. Typing "gprof | more" instead of just "gprof" will ease readability.
g++ -pg -ocrunch crunch.cc crunch gprofThis produces a lot of output. The most important information is at the start.
profile on spdemo profile plot profile offWithin the department one particular diagnostic session led to a speedup of 3000 times (to several hours to several seconds) when it turned out that the same huge .mat file was being unintentionally loaded on every iteration of a loop.
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.ccthe 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.
| | computing help | memory optimisation | Faster, longer programs, and optimisation | |