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
Improve performance of pathlib.Path.absolute() #100562
Labels
3.12
expert-pathlib
performance
Performance or resource usage
type-feature
A feature request or enhancement
Comments
barneygale
added a commit
to barneygale/cpython
that referenced
this issue
Dec 28, 2022
Increase performance of the `absolute()` method by calling `os.getcwd()` directly, rather than using the `Path.cwd()` class method. This avoids constructing an extra `Path` object (and the parsing/normalization that comes with it). Decrease performance of the `cwd()` class method by calling the `Path.absolute()` method, rather than using `os.getcwd()` directly. This involves constructing an extra `Path` object. We do this to maintain a longstanding pattern where `os` functions are called from only one place, which allows them to be more readily replaced by users. As `cwd()` is generally called at most once within user programs, it's a good bargain.
AlexWaygood
added a commit
to barneygale/cpython
that referenced
this issue
Jan 5, 2023
miss-islington
pushed a commit
that referenced
this issue
Jan 5, 2023
Increase performance of the `absolute()` method by calling `os.getcwd()` directly, rather than using the `Path.cwd()` class method. This avoids constructing an extra `Path` object (and the parsing/normalization that comes with it).
Decrease performance of the `cwd()` class method by calling the `Path.absolute()` method, rather than using `os.getcwd()` directly. This involves constructing an extra `Path` object. We do this to maintain a longstanding pattern where `os` functions are called from only one place, which allows them to be more readily replaced by users. As `cwd()` is generally called at most once within user programs, it's a good bargain.
```shell
# before
$ ./python -m timeit -s 'from pathlib import Path; p = Path("foo", "bar")' 'p.absolute()'
50000 loops, best of 5: 9.04 usec per loop
# after
$ ./python -m timeit -s 'from pathlib import Path; p = Path("foo", "bar")' 'p.absolute()'
50000 loops, best of 5: 5.02 usec per loop
```
Automerge-Triggered-By: GH:AlexWaygood
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
3.12
expert-pathlib
performance
Performance or resource usage
type-feature
A feature request or enhancement
barneygale commentedDec 28, 2022
•
edited by bedevere-bot
The current implementation of
pathlib.Path.absolute()callsself.cwd()rather thanos.getcwd(), and so constructs twoPathobjects rather than one. As path objects are slow to construct, this has a performance impact.Linked PRs
pathlib.Path.absolute()#100563The text was updated successfully, but these errors were encountered: