If you have a heavy logging application, you might want to take into consideration to use the %-format strings for logging.
The Python logging module recommends to use the %-format.
But I guess most of the developers will prefer the f-string format, which came with the Python 3.6 release, because it simply easier to read.
My question was: Which impact does it have on performance? In case, we use logging with string concatenation.
So, I wrote a short script to “benchmark” it (and yes, I know it is not a proper benchmark).
Compared was: logging and concatenation with ‘+’ vs formatting with % vs f-strings, over multiple runs in a different order of the benchmark functions. So that, we can at least exclude the cold start issue.
If we take an average of all measuring points and compare them with each other, we would come to the result:
|formatting with %||concatenation with +||f-strings|
|7.087545455 sec||7.588454545 sec||7.707727273 sec|
That means that, formatting with % is 8.75% faster than with f-string or 7.06% faster than with concatenation. At least in the combination with “deactivated” logging.
The reason for that is probably, because the string will be concatenated anyway, even if we don’t log anything. But in case of formatting with %, the string will be not concatenated.