Debug and preprocessor directive

For debugging, I have many calls to the debug log function in my application. Of course, in the production version, these debugging calls must be skipped. Instead of writing:

#if DEVEL == 1 Log::debug(...); #endif 

around all the calls to the debug function, I decided to write the following in the debug function itself:

 #if DEVEL != 1 return; #endif 

Will there be an exception due to useless use of the function call by the compiler, or am I better off using the (many ugly) #if #endif construct for performance reasons?

+6
source share
3 answers

Instead of worrying about the 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 make it simple.

+7
source

If a function is available for embedding (for example, it is implemented in the header), then the optimizer will not be able to get rid of the function call and, thus, will not have overhead.

+5
source

Why aren't you checking?

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

+2
source

Source: https://habr.com/ru/post/890328/


All Articles