Skip to content

Instantly share code, notes, and snippets.

@xflr6
Last active June 4, 2022 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xflr6/c58c7c022d90a72ccd8e7d6510a6cc78 to your computer and use it in GitHub Desktop.
Save xflr6/c58c7c022d90a72ccd8e7d6510a6cc78 to your computer and use it in GitHub Desktop.
Compare subdirectory generator using os.walk() with one using scandir.scandir()
"""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