Skip to content

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
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