Skip to content

Instantly share code, notes, and snippets.

@typemytype
Created November 15, 2017 07:56
Show Gist options
  • Select an option

  • Save typemytype/eb69d0a4b6acd92241af9d97e0bb3baa to your computer and use it in GitHub Desktop.

Select an option

Save typemytype/eb69d0a4b6acd92241af9d97e0bb3baa to your computer and use it in GitHub Desktop.
import time
import os
repeat = 10000
notExistingPath = u"test-non-existing.txt"
existingPath = 'test-exsisting.txt'
f = open(existingPath, "w")
f.write("hello world")
f.close()
def test(info, paths):
print(info)
t = time.time()
for i in range(repeat):
for path in paths:
if not os.path.exists(path):
continue
file = open(path)
file.read()
r = time.time() - t
print(r)
t = time.time()
for i in range(repeat):
for path in paths:
try:
with open(path) as file:
file.read()
except IOError:
continue
r = time.time() - t
print(r)
print()
test("non exists", [notExistingPath, notExistingPath])
test("mixed", [notExistingPath, existingPath])
test("all exists", [existingPath, existingPath])
os.remove(existingPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment