Skip to content

Instantly share code, notes, and snippets.

@csomh
Created March 18, 2021 15:54
Show Gist options
  • Save csomh/ae177bc9b23ce519bd59b1af6b7c9f5d to your computer and use it in GitHub Desktop.
Save csomh/ae177bc9b23ce519bd59b1af6b7c9f5d to your computer and use it in GitHub Desktop.
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