Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python_assert_debug_logging - benchmark, using python assert to discard function calls and arguments evaluation
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
You can’t perform that action at this time.