Skip to content

Instantly share code, notes, and snippets.

@mikeckennedy
Created September 29, 2021 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeckennedy/f516c9cf2f7a69a02a815e3799b42f95 to your computer and use it in GitHub Desktop.
Save mikeckennedy/f516c9cf2f7a69a02a815e3799b42f95 to your computer and use it in GitHub Desktop.
import datetime
def run_with_no_ex(count: int):
idx = 0
while idx < count:
idx += 1
s = "".upper()
def run_with_ex(count: int):
idx = 0
while idx < count:
idx += 1
try:
s = "".upper()
except Exception as x:
print(f"Error! {x}")
def time(func, count, msg):
t0 = datetime.datetime.now()
func(count)
dt = datetime.datetime.now() - t0
print(f'Finished {msg} in {dt.total_seconds()*1000:,.0f}ms')
if __name__ == '__main__':
run_with_ex(1)
run_with_no_ex(1)
time(run_with_no_ex, 1_000_000*100, "No exception block")
time(run_with_ex, 1_000_000*100, "Exception block")
@mikeckennedy
Copy link
Author

Output seems pretty much tied for me:

Finished No exception block in 7,465 ms
Finished Exception block in 7,894 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment