Skip to content

Instantly share code, notes, and snippets.

@guyarad
Last active November 1, 2016 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guyarad/37be575febbed21bc4868a075e957354 to your computer and use it in GitHub Desktop.
Save guyarad/37be575febbed21bc4868a075e957354 to your computer and use it in GitHub Desktop.
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("log")
def simple():
if logger.isEnabledFor(logging.DEBUG):
logger.debug('Stupid log message ' + ' '.join([str(i) for i in range(20)]))
class LazyJoin(object):
def __init__(self, s=None, items=None):
self.__call__(s, items)
def __call__(self, s, items):
self.s = s
self.items = items
def __str__(self):
return self.s.join(self.items)
def no_lazy():
logger.debug('Stupid log message %s', ' '.join([str(i) for i in range(20)]))
def original_lazy():
logger.debug('Stupid log message %s',
LazyJoin(' ', (str(i) for i in range(20))))
def callable_lazy(lazy_log):
logger.debug('Stupid log message %s', lazy_log(' ', (str(i) for i in range(20))))
def no_arguments_lazy(lazy_log):
logger.debug('Stupid log message %s', lazy_log)
if __name__ == '__main__':
import timeit
print(min(timeit.repeat("simple()", setup="from __main__ import simple")))
print(min(timeit.repeat("no_lazy()", setup="from __main__ import no_lazy")))
print(min(timeit.repeat("original_lazy()", setup="from __main__ import original_lazy")))
print(min(timeit.repeat("callable_lazy(lazy_log)", setup="from __main__ import callable_lazy, LazyJoin; "
"lazy_log = LazyJoin()")))
print(min(timeit.repeat("no_arguments_lazy(lazy_log)",
setup="from __main__ import no_arguments_lazy, LazyJoin; "
"lazy_log = LazyJoin(' ', (str(i) for i in range(20)))")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment