Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-117727: Speed up pathlib.Path.iterdir() by using os.scandir() #117728

Merged
merged 4 commits into from
Apr 12, 2024

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Apr 10, 2024

Replace use of os.listdir() with os.scandir(). Forgo setting _drv, _root and _tail_cached, as these usually aren't needed. Use os.DirEntry.path to set _str.

Timing:

$ ./python -m timeit -s "from pathlib import Path; p = Path('Lib')" "list(p.iterdir())"
1000 loops, best of 5: 303 usec per loop  # before
1000 loops, best of 5: 257 usec per loop  # after
# --> 1.18x faster

…ir()`

Replace use of `os.listdir()` with `os.scandir()`. Forgo setting `_drv`,
`_root` and `_tail_cached`, as these usually aren't needed. Use
`os.DirEntry.path` to set `_str`.
@barneygale barneygale enabled auto-merge (squash) April 12, 2024 21:29
@barneygale barneygale merged commit 30f0643 into python:main Apr 12, 2024
33 checks passed
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
…ir()` (python#117728)

Replace use of `os.listdir()` with `os.scandir()`. Forgo setting `_drv`,
`_root` and `_tail_cached`, as these usually aren't needed. Use
`os.DirEntry.path` to set `_str`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage topic-pathlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants