test.py
import logging
# V1
logging.debug('%s before you %s', 'Look', 'leap!')
# V2
logging.debug('{} before you {}'.format('Look', 'leap!'))
I have the following code above.
Is it true when I execute with python test.py --log=INFO
V2 will incur an extra CPU cycle because it will first format the string and then not output anything because of the debug level.
Is there anyway to use .format
styling with logging and not incur this extra CPU cycle if the log ultimately isn't outputted?
EDIT: Part 1 -> Yes it's true via this testing code (Although very small)
old-format.py
import logging
# V1
for x in range(0, 100000):
logging.debug('%s before you %s', 'Look', 'leap!')
new-format.py
import logging
# V2
for x in range(0, 100000):
logging.debug('{} before you {}'.format('Look', 'leap!'))
python -m cProfile ~/Desktop/old-format.py
-> 500464 function calls in 0.176 seconds
python -m cProfile ~/Desktop/new-format.py
-> 600464 function calls in 0.237 seconds