Skip to content

Instantly share code, notes, and snippets.

Last active November 1, 2016 12:31
What would you like to do?
import logging
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()")))
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