Updated almost 12 years ago by Knödlseder Jürgen
Method-call overheads¶
The method-call overhead is the time spent in passing arguments to a method and retrieving the return value. Normally, the method-call overhead is only relevant for methods that perform very little computations, so that a substantial fraction of the time is spent by setting up the method call.
To illustrate the method-call overhead, check the small attached program arguments.cpp / arguments.hpp. The program can be compiled using for example
$ g++ -o arguments arguments.cppThe program tests passing 5 or 15 arguments in 3 different ways:
- Passing 5 or 15 int arguments by value
- Passing 5 or 15 int arguments by reference
- Passing 5 or 15 int arguments collected in a single structure by reference (which comes back to passing a single argument)
5 | 15 | |
By value | 0.86 | 1.73 |
By reference | 0.70 | 1.64 |
Using a structure | 0.30 | 0.34 |
Obviously, passing a single structure as argument produces the smallest method-call overhead. Passing by reference is a little faster than passing by value, even for int values.
The general rules derived from this experiment are:- Arguments should be passed by reference.
- The number of arguments should be as small as possible. When necessary, group the arguments in a single structure.
5 | 15 | |
By value | 1.02 | 2.15 |
By reference | 0.83 | 2.19 |
Using a structure | 0.46 | 0.98 |