3 ms·
Logging is never free. Either you add additional compile time cost to remove it, or you add at the very least a single if and/or method call at runtime. It's n
by hvidgaard 9y ago
Logging is never free. Either you add additional compile time cost to remove it, or you add at the very least a single if and/or method call at runtime.
It's not easy problem to solve. The best I've come up with so far, is using a decorator in conjunction with a DI framework that supports it. It's still only a wrap around method calls, but it enables completely turning off logging in production, and enable it when it is needed without making a special build, or wasting resources during normal execution.
- kbutler 9y agoPreprocessor-disabled logging (and generally compile-time disabled logging) is free at run-time. The compile-time cost of stripping out preprocessor macros disabled via #ifdef is close enough to free you'd have a really hard time measuring it - O(n) on number of lines of logging code, with n smaller than the number of lines of actual code for any real scenario, and a very small multiplier. The "cost" there is in the visual appearance of the source code.