Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging and preprocessor directive

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?

like image 320
Marcello Avatar asked Jun 12 '11 07:06

Marcello


3 Answers

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.

like image 87
iammilind Avatar answered Oct 27 '22 00:10

iammilind


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.

like image 5
Motti Avatar answered Oct 26 '22 22:10

Motti


Why don't you check?

With gcc just compile with -S, and look at the output.

like image 2
Gilad Naor Avatar answered Oct 27 '22 00:10

Gilad Naor