![[Dept of Engineering]](http://www.eng.cam.ac.uk/images/house_style/engban-s.gif)
Next: Debugging
Up: More C++
Previous: Limits
The quality of the compiler and libraries can greatly affect the
speed of the resulting code, but there's a lot you can do to
help.
- Read about the available optimiser options. On HP's for example,
using '+O4' instead of no optimisation can speed up some code by orders
of magnitude.
- Try not to use call-by-value for big objects. Unless
you need to change the object, use a reference to a const value
- Some C++ features have compile time implications, others have
run time, program size or debugging implications. For example, making a function
virtual can add 15% to the cost of a function call whereas
templates have no runtime implications. They do, however, make things
more difficult for debuggers and compilers. The Standard Template Library,
for example, avoids virtual functions for performance reasons.
- Inlined routines are faster, but they'll bloat the code. Note
that a member function defined in the class declaration
is automatically inline. In particular, note that an inline destructor
can be inserted at each exit point of a function.
- Though Standard Library algorithms like reverse work
on both list and vector containers, the
choice of container is still important for performance.
- Though vectors can grow, it's more efficient to
create them with their maximum size.
- Try to create and initialise a variable in one operation.
Next: Debugging
Up: More C++
Previous: Limits
Tim Love
2001-07-05