Created
November 15, 2017 07:56
-
-
Save typemytype/eb69d0a4b6acd92241af9d97e0bb3baa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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