← 返回首页
gh-89727: Improve os.walk complexity and speed by zmievsa · Pull Request #100671 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

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

Provide feedback

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

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

gh-89727: Improve os.walk complexity and speed#100671

Merged
JelleZijlstra merged 5 commits into
python:mainfrom
zmievsa:gh-89727/minor-os-walk-refactoring
Jan 2, 2023
Merged

gh-89727: Improve os.walk complexity and speed#100671
JelleZijlstra merged 5 commits into
python:mainfrom
zmievsa:gh-89727/minor-os-walk-refactoring

Conversation

zmievsa commented Jan 1, 2023

Copy link
Copy Markdown
Contributor

Use isinstance for checking the top of the stack in os.walk to simplify it and speed it up.

A small benchmark below. "." is this repository.

AlexWaygood added the performance Performance or resource usage label Jan 2, 2023

zmievsa commented Jan 2, 2023
edited
Loading

Copy link
Copy Markdown
Contributor Author

@JelleZijlstra I can't assign a reviewer but you merged the prior PR related to os.walk so I decided that it makes sense to ping you here. I hope that's ok :))

JelleZijlstra self-requested a review January 2, 2023 03:40
Comment thread Misc/NEWS.d/next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst Outdated Show resolved Hide resolved

Copy link
Copy Markdown
Member

Do you have a benchmark that shows this actually makes it faster? We lose a tuple allocation but gain a function call, so it's not completely obvious this should be faster.

…edHN.rst Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>

zmievsa commented Jan 2, 2023
edited
Loading

Copy link
Copy Markdown
Contributor Author

I made this MR because my initial benchmarking with pathlib.Path.walk and os.walk was quite obvious. However, after timing it a few more times I have gotten inconsistent results.

Seems like the isinstance version is just a tiny bit (insignificantly) faster but I would still argue that it makes more sense than the original because it is slightly simpler.

But what do you think?

Copy link
Copy Markdown
Member

Thanks, seems like it's about a wash but since the new code is simpler and we never released the old version, seems fine to change this.

JelleZijlstra merged commit 73097d9 into python:main Jan 2, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Footer

© 2026 GitHub, Inc.