Movatterモバイル変換


[0]ホーム

URL:


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 upos.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
encukou merged 3 commits intopython:mainfrombarneygale:gh-119186-joining
Sep 3, 2025

Conversation

@barneygale
Copy link
Contributor

@barneygalebarneygale commentedJul 6, 2024
edited
Loading

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

(this trick is already used inos.fwalk())

$ ./python -m timeit -s'import os''list(os.walk("."))'5 loops, best of 5: 41.4 msec per loop5 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 concatenationthereafter to generate child paths.
@barneygalebarneygale added the performancePerformance or resource usage labelJul 6, 2024
@barneygalebarneygale changed the titleGH-119186: Slightly speed upos.walk(topdown=True)GH-119169: Slightly speed upos.walk(topdown=True)Jul 6, 2024
# above.
iffollowlinksornotislink(new_path):
stack.append(new_path)
ifdirs:
Copy link
Contributor

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 ofos.join and how much due to the check on emptydirs? On my system a walk of a folder with github repositories (including the cpython repo) hasdirs empty about 60% of the times.

@encukouencukou merged commitb19ad11 intopython:mainSep 3, 2025
45 checks passed
lkollar pushed a commit to lkollar/cpython that referenced this pull requestSep 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 concatenationthereafter to generate child paths.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@eendebakpteendebakpteendebakpt left review comments

Assignees

No one assigned

Labels

performancePerformance or resource usage

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@barneygale@eendebakpt@encukou

[8]ページ先頭

©2009-2025 Movatter.jp