Last active
November 14, 2017 20:44
-
-
Save ipmb/42adbb26e3e6a797a493a003cfdca428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Example of the speed-up allowed by logging's string handling""" | |
from __future__ import division | |
import logging | |
import string | |
import time | |
logger = logging.getLogger(__name__) | |
logger.addHandler(logging.StreamHandler()) | |
logger.setLevel(logging.INFO) | |
# make a big object that is slow to turn into a string | |
big_obj = {} | |
obj = big_obj | |
for x in string.ascii_letters * 1000: | |
obj[x] = {} | |
obj = obj[x] | |
def time_log(level): | |
start = time.time() | |
getattr(logger, level)('%s: This is a big object: %s', level, big_obj) | |
end = time.time() | |
return end - start | |
# this is not logged | |
ignored_log = time_log('debug') | |
# this is logged | |
logged_log = time_log('info') | |
print(""" | |
unlogged time: {}s | |
logged time: {}s | |
unlogged time is {:.0f}x faster | |
""".format(ignored_log, logged_log, logged_log / ignored_log)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unlogged time: 3.0040740966796875e-05s | |
logged time: 1.5769679546356201s | |
unlogged time is 52494x faster |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seg fault!!!