Created
March 18, 2021 15:54
-
-
Save csomh/ae177bc9b23ce519bd59b1af6b7c9f5d to your computer and use it in GitHub Desktop.
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
import timeit | |
files = [f"file{i}" for i in range(300)] | |
def has_spec_for(files): | |
for file in files: | |
if file.endswith(".spec"): | |
return True | |
return False | |
def has_spec_any(files): | |
return any(file.endswith(".spec") for file in files) | |
files[10] = "file.spec" | |
print("for loop, spec-file file in the beginning of the list:", timeit.timeit(lambda: has_spec_for(files))) | |
print("any, spec-file file in the beginning of the list:", timeit.timeit(lambda: has_spec_any(files))) | |
files[10] = "file10" | |
files[100] = "file.spec" | |
print("for loop, spec-file file in the middle of the list:", timeit.timeit(lambda: has_spec_for(files))) | |
print("any, spec-file file in the middle of the list:", timeit.timeit(lambda: has_spec_any(files))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment