I'm optimizing a Python program that performs some sort of calculation. It uses NumPy quite extensively. The code is sprinkled with logger.debug calls (logger is the standard Python log object).
When I run cProfile I see that Numpy's function that converts an array to string takes 50% of the execution time. This is surprising, since there is no handler that outputs messages as the DEBUG level, only INFO and above.
Why is the logger converting its arguments to string even though nobody is going to use this string? Is there a way to prevent it (other than not performing the logger calls)?
Use logger.debug('%s', myArray) rather than logger.debug(myArray). The first argument is expected to be a format string (as all the documentation and examples show) and is not assumed to be computationally expensive. However, as @dwanderson points out, the logging will actually only happen if the logger is enabled for the level.
Note that you're not forced to pass a format string as the first parameter - you can pass a proxy object that will return the string when str() is called on it (this is also documented). In your case, that's what'll probably do the array to string conversion.
If you use logging in the manner that you're supposed to (i.e. as documented), there shouldn't be the problem you describe here.
Python logger.debug converting arguments to string without logging