import timeit
print timeit.Timer("foo(1)", setup="""
def foo(x):
if x:
return None
else:
return x
""").repeat()
print timeit.Timer("bar(1)", setup="""
def bar(x):
if x:
return None
else:
raise NotImplementedError
""").repeat()
The formar function doesn't raise any exceptions. The latter does. The timeit module measures time to run given code 1000000 times, and repeat it 3 times. The result is below:
[0.24111700057983398, 0.22863888740539551, 0.22955012321472168]
[0.23151803016662598, 0.23359298706054688, 0.2297508716583252]
I concluded there is no extra overhead between them.
No comments:
Post a Comment