Reduce Function-Call Overhead
Whether you are writing a function or calling a library function, there are a few
things you should keep in mind:
Use virtual functions only when necessary. They are
usually compiled to be indirect calls, which are slower than direct calls.
Usually, you should not declare virtual functions
inline. If all virtual functions in a class are inline, the virtual function table and all
the virtual function bodies will be replicated in each compilation unit that uses the
class.
- Unless absolutely necessary, do not use function pointers; call functions directly.
- Fully prototype all functions. A full prototype gives the compiler and optimizer
complete information about the types of the parameters. As a result, promotions from
unwidened types to widened types are not required and the compiler never needs to emit
eyecatcher instructions for the function.
- Use _Optlink linkage wherever possible. Keep _Optlink as your default linkage and use
linkage keywords to change the linkage for specific functions.
- Avoid using unprototyped variable argument functions. Because of the nature of the
_Optlink calling convention, unprototyped variable-length argument lists make performance
slower. Also, use the _System calling convention for any variable argument functions.
- When designing a function, place the most used parameters in the left-most position in
the function prototype. The left-most parameters have a better chance of being stored in a
register. For the same reason, parameters used immediately upon function entry should also
be placed near the left-most position (the first three integral parameters can be passed
via parameters).
- Avoid passing structures or unions as function parameters or returning a structure or a
union. Passing such aggregates requires the compiler to copy and store many values. This
is worse in C++ programs in which class objects are passed by value, since a constructor
and destructor are called when the function is called. Instead, pass or return a pointer
to the structure or union, or pass it by reference.
- Pass atomic types (like int and short) by value rather than passing by reference,
wherever possible.
- If you call another function near the end of your function and pass it the same
parameters that were passed to your function, put the parameters in the same order in the
function prototypes. The compiler can then reuse the storage that the parameters are in
and does not have to generate code to reorder them.
- Use the intrinsic and built-in functions, which include string manipulation,
floating-point, and trigonometric functions, instead of coding your own. Intrinsic
functions require less overhead and are faster than a function call, and often allow the
compiler to perform better optimization. Your functions are automatically mapped to
intrinsic functions if you include the VisualAge C++ header files.
This mapping is overridden if you #undef the macro name.
- You can increase processing speed by inlining certain math functions with the _FP_INLINE macro.When you define _FP_INLINE, the
prototypes for the trigonometric functions (sin, cos, tan, asin, acos, atan)and sqrt are
modified so that the built-in functions are used instead of the run-time library
equivalents.
Note: you cannot take advantage of error handling when _FP_INLINE is
defined. It is assumed that errno is never set or called.
- Use recursion only where necessary. Because recursion involves building a stack frame,
an iterative solution is always faster than a recursive one.

Overview of Optimization
Calling Conventions

Optimize Your Application

C Library Reference - Intrinsic Functions