Created
August 17, 2016 06:41
-
-
Save Jimilian/44b240aeb44174f9cf768a2dc18bcfee to your computer and use it in GitHub Desktop.
In some cases exception-way could be much faster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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