Last active
November 1, 2016 12:31
-
-
Save guyarad/37be575febbed21bc4868a075e957354 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
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