Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-119169: Slightly speed up os.walk(topdown=True) #121431

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

Merged
encukou merged 3 commits into python:main from barneygale:gh-119186-joining
Sep 3, 2025

Conversation

Copy link
Contributor

@barneygale barneygale commented Jul 6, 2024
edited
Loading

When os.walk() traverses into subdirectories in top-down mode, call os.path.join() once to add a trailing slash, and use string concatenation thereafter to generate child paths.

(this trick is already used in os.fwalk())

$ ./python -m timeit -s 'import os' 'list(os.walk("."))'
5 loops, best of 5: 41.4 msec per loop
5 loops, best of 5: 40.7 msec per loop
# --> 1.7% faster

When `os.walk()` traverses into subdirectories in top-down mode, call
`os.path.join()` once to add a trailing slash, and use string concatenation
thereafter to generate child paths.
@barneygale barneygale added the performance Performance or resource usage label Jul 6, 2024
@barneygale barneygale changed the title (削除) GH-119186: Slightly speed up os.walk(topdown=True) (削除ここまで) (追記) GH-119169: Slightly speed up os.walk(topdown=True) (追記ここまで) Jul 6, 2024
# above.
if followlinks or not islink(new_path):
stack.append(new_path)
if dirs:
Copy link
Contributor

@eendebakpt eendebakpt Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much of the gain is due to the elimination of os.join and how much due to the check on empty dirs? On my system a walk of a folder with github repositories (including the cpython repo) has dirs empty about 60% of the times.

@encukou encukou merged commit b19ad11 into python:main Sep 3, 2025
45 checks passed
lkollar pushed a commit to lkollar/cpython that referenced this pull request Sep 9, 2025
...121431)
pythonGH-119186: Slightly speed up `os.walk(topdown=True)`
When `os.walk()` traverses into subdirectories in top-down mode, call
`os.path.join()` once to add a trailing slash, and use string concatenation
thereafter to generate child paths.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@eendebakpt eendebakpt eendebakpt left review comments

Assignees
No one assigned
Labels
performance Performance or resource usage
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /