Skip to content

Instantly share code, notes, and snippets.

@fnielsen
Created July 31, 2013 23:21
Show Gist options
  • Save fnielsen/6127124 to your computer and use it in GitHub Desktop.
Save fnielsen/6127124 to your computer and use it in GitHub Desktop.
This is a modified version of the timing code by Patrick Altman "Try / Except Performance in Python: A Simple Test" http://paltman.com/2008/01/18/try-except-performance-in-python-a-simple-test/
"""
This is a modified version of the timing code by
Patrick Altman "Try / Except Performance in Python: A Simple Test"
http://paltman.com/2008/01/18/try-except-performance-in-python-a-simple-test/
"""
import time
def time_me(function):
def wrap(*arg):
start = time.time()
r = function(*arg)
end = time.time()
print("{} ({:.3f} ms)".format(function.__name__, (end-start)*1000))
return r
return wrap
# Not Existing
@time_me
def with_try(iterations):
d = {'somekey': 123}
for i in range(0, iterations):
try:
pass
except KeyError:
pass
@time_me
def with_try_exc(iterations):
d = {'somekey': 123}
for i in range(0, iterations):
try:
get = d['notexist']
except KeyError:
get = d['somekey']
@time_me
def without_try(iterations):
d = {'somekey': 123}
for i in range(0, iterations):
if 'notexist' in d:
pass
else:
pass
@time_me
def without_try_exc(iterations):
d = {'somekey': 123}
for i in range(0, iterations):
if 'notexist' in d:
get = d['notexist']
else:
get = d['somekey']
@time_me
def without_try_not(iterations):
d = {'somekey': 123}
for i in range(0, iterations):
if 'notexist' not in d:
pass
else:
pass
# Existing
@time_me
def exists_with_try(iterations):
d = {'somekey': 123}
for i in range(0, iterations):
try:
pass
except KeyError:
pass
@time_me
def exists_with_try_exc(iterations):
d = {'somekey': 123}
for i in range(0, iterations):
try:
get = d['somekey']
except KeyError:
get = d['somekey']
@time_me
def exists_without_try(iterations):
d = {'somekey': 123}
for i in range(0, iterations):
if 'somekey' in d:
pass
else:
pass
@time_me
def exists_without_try_exc(iterations):
d = {'somekey': 123}
for i in range(0, iterations):
if 'somekey' in d:
get = d['somekey']
else:
get = d['somekey']
@time_me
def exists_without_try_not_exc(iterations):
d = {'somekey': 123}
for i in range(0, iterations):
if 'somekey' not in d:
get = d['somekey']
else:
get = d['somekey']
print("The case where the key does not exist:")
print("1,000 iterations:")
with_try(1000)
with_try_exc(1000)
without_try(1000)
without_try_exc(1000)
without_try_not(1000)
print("\n100,000 iterations:")
with_try(100000)
with_try_exc(100000)
without_try(100000)
without_try_exc(100000)
without_try_not(100000)
print("\n1,000,000 iterations:")
with_try(1000000)
with_try_exc(1000000)
without_try(1000000)
without_try_exc(1000000)
without_try_not(1000000)
print("\n\nThe case where the key does exist:")
print("1,000 iterations:")
exists_with_try(1000)
exists_with_try_exc(1000)
exists_without_try(1000)
exists_without_try_exc(1000)
exists_without_try_not_exc(1000)
print("\n100,000 iterations:")
exists_with_try(100000)
exists_with_try_exc(100000)
exists_without_try(100000)
exists_without_try_exc(100000)
exists_without_try_not_exc(100000)
print("\n1,000,000 iterations:")
exists_with_try(1000000)
exists_with_try_exc(1000000)
exists_without_try(1000000)
exists_without_try_exc(1000000)
exists_without_try_not_exc(1000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment