Last active
June 4, 2022 13:51
-
-
Save xflr6/c58c7c022d90a72ccd8e7d6510a6cc78 to your computer and use it in GitHub Desktop.
Compare subdirectory generator using os.walk() with one using scandir.scandir()
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
"""Compare two ways to iterate over subdirectories of a tree.""" | |
from collections.abc import Iterator | |
import os | |
import platform | |
import time | |
START_DIR = 'c:\\Users' if platform.system() == 'Windows' else '/usr' | |
def itersubdirs_walk(start_dir: os.PathLike | str) -> Iterator[str]: | |
for root, dirs, _ in os.walk(start_dir): | |
for d in dirs: | |
yield os.path.join(root, d) | |
def itersubdirs_scandir(start_dir: os.PathLike | str) -> Iterator[str]: | |
stack = [start_dir] | |
while stack: | |
root = stack.pop() | |
try: | |
dentries = os.scandir(root) | |
except OSError: | |
continue | |
dirs = [] | |
for d in dentries: | |
if d.is_dir(): | |
yield d.path | |
dirs.append(d.path) | |
stack.extend(reversed(dirs)) | |
results = [] | |
for func in (itersubdirs_walk, itersubdirs_scandir): | |
print(func) | |
start = time.perf_counter_ns() | |
result = list(func(START_DIR)) | |
results.append(result) | |
print((time.perf_counter_ns() - start) / 1_000_000_000) | |
print(*result[:20], sep='\n') | |
print(len(result)) | |
assert results[0] == results[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment