Created
July 19, 2011 18:34
-
-
Save rotoglup/1093358 to your computer and use it in GitHub Desktop.
python_assert_debug_logging - benchmark, using python assert to discard function calls and arguments evaluation
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 contextlib | |
import time | |
@contextlib.contextmanager | |
def time_print(prefix): | |
t0 = time.time() | |
yield | |
t1 = time.time() | |
print "'%s' time: %fmsec" % (prefix, (t1-t0)*1000) | |
def DBG_empty(message): | |
""" | |
Release version of debug logging function: do nothing | |
""" | |
pass | |
def DBG(message): | |
""" | |
Debug logging function | |
""" | |
return True | |
ITERATIONS = 30000 | |
ENABLE_DEBUG_MESSAGES = False | |
SOME_LIST = xrange(8) | |
with time_print("pass"): | |
for x in xrange(ITERATIONS): | |
pass | |
with time_print("empty logging function"): | |
for x in xrange(ITERATIONS): | |
DBG_empty( "some message with costy parameter : '%s'" + str( [x for x in SOME_LIST ] ) ) | |
with time_print("if __debug__"): | |
for x in xrange(ITERATIONS): | |
if __debug__: | |
DBG("some message with costy parameter : '%s'" + str( [x for x in SOME_LIST ] )) | |
with time_print("if ENABLE_DEBUG_MESSAGES"): | |
for x in xrange(ITERATIONS): | |
if ENABLE_DEBUG_MESSAGES: | |
DBG("some message with costy parameter : '%s'" + str( [x for x in SOME_LIST ] )) | |
with time_print("assert logging function"): | |
for x in xrange(ITERATIONS): | |
assert DBG("some message with costy parameter : '%s'" + str( [x for x in SOME_LIST ] )) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment