This does not change the way the function is called. Probably not so much if you want to be able to profile each function.
static inline int real_function() { // previous contents of function(), with no tic or toc } int function() { tic(); int r = real_function(); toc(); return r; }
As everyone says: use a profiler, this will save you a lot of effort in the long run. As they do not say: if your platform has one.
If this is not the case, then the easiest way to say (usually coding) is that functions should have only one exit point, and the exit point should be through your macro. Then you can manually use all your functions with the code when you enter and exit. Inherited multi-return functions can be wrapped as described above.
Also, keep in mind when you do something like this that your compiler can ruin you. You can write this:
tic(); do_something(); int i = something_else(); toc(); return i;
If the compiler determines that something_else has no side effects, then although something_else takes a considerable amount of time, it can turn the code into this:
tic(); do_something(); toc(); return something_else();
And your profile data will underestimate the time spent on your function. Another reason it is so good to have a real profiler is that it can work with the compiler.
source share