For debugging I have many calls to a debug log function in my application. Of course in the production version these debugging calls need to be skipped. Instead of writing:
#if DEVEL == 1
Log::debug(...);
#endif
around all calls to the debug function I decided the write the following in the debug function itself:
#if DEVEL != 1
return;
#endif
Will the overhead of the useless function call be avoided by the compiler or am I better off by using (many ugly) #if #endif
construction for performance reasons?
Instead of worrying about optimizer, you can do a simple trick:
#if DEVEL == 1
#define LOG_DEBUG(...) Log::Debug(__VA_ARGS__) // variadic macro
#else
#define LOG_DEBUG
#endif
Now use LOG_DEBUG
everywhere to keep it simple.
If the function is available for inlining (e.g. it is implemented in the header) then the optimizer will have no trouble getting rid of the function call and thus leave you with no overhead.
Why don't you check?
With gcc just compile with -S
, and look at the output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With