Skip to content

Instantly share code, notes, and snippets.

@Jimilian
Created August 17, 2016 06:41
Show Gist options
  • Save Jimilian/44b240aeb44174f9cf768a2dc18bcfee to your computer and use it in GitHub Desktop.
Save Jimilian/44b240aeb44174f9cf768a2dc18bcfee to your computer and use it in GitHub Desktop.
In some cases exception-way could be much faster
from __future__ import print_function
import os
import timeit
test1="""
def check_file_classic():
if os.path.exists("asdfas"):
return 1
return 2
"""
test2="""
def check_file_with_exception():
try:
open("asdfas")
except:
return 2
"""
iterations=10000
print("Classic")
print(">>", timeit.timeit(test1, number=iterations))
print("Exception")
print(">>", timeit.timeit(test2, number=iterations))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment