Thanks to visit codestin.com
Credit goes to github.com

Skip to content

GH-112727: Speed up pathlib.Path.absolute() #112728

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
merged 4 commits into from
Dec 4, 2023

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Dec 4, 2023

Use _from_parsed_parts() to create a pre-joined/pre-parsed path, rather than passing multiple arguments to with_segments()

Use `_from_parsed_parts()` to create a pre-joined/pre-parsed path, rather
than passing multiple arguments to `with_segments()`
@barneygale
Copy link
Contributor Author

barneygale commented Dec 4, 2023

1.5x speedup for paths with tail segments:

$ ./python -m timeit -s "from pathlib import Path" "str(Path().absolute())"
100000 loops, best of 5: 3.82 usec per loop  # before
 50000 loops, best of 5: 3.9  usec per loop  # after

$ ./python -m timeit -s "from pathlib import Path" "str(Path('foo').absolute())"
50000 loops, best of 5: 9.79 usec per loop  # before
50000 loops, best of 5: 6.2  usec per loop  # after

$ ./python -m timeit -s "from pathlib import Path" "str(Path('foo', 'bar').absolute())"
20000 loops, best of 5: 11.5  usec per loop  # before
50000 loops, best of 5:  7.54 usec per loop  # after

It would be interesting to also test the performance of str(WindowsPath('c:foo').absolute()) and str(WindowsPath('/foo').absolute()), but I don't have a Windows box to hand.

@AlexWaygood
Copy link
Member

Big speedups on Windows!

On main:

>python -m timeit -s "from pathlib import WindowsPath" "str(WindowsPath('c:foo').absolute())"
Running PGUpdate|x64 interpreter...
50000 loops, best of 5: 9.92 usec per loop

>python -m timeit -s "from pathlib import WindowsPath" "str(WindowsPath('/foo').absolute())"
Running PGUpdate|x64 interpreter...
50000 loops, best of 5: 8.32 usec per loop

With PR:

>python -m timeit -s "from pathlib import WindowsPath" "str(WindowsPath('c:foo').absolute())"
Running PGUpdate|x64 interpreter...
50000 loops, best of 5: 6.01 usec per loop

>python -m timeit -s "from pathlib import WindowsPath" "str(WindowsPath('/foo').absolute())"
Running PGUpdate|x64 interpreter...
50000 loops, best of 5: 4.97 usec per loop

(Timings done using a reasonably fresh PGO-optimised build)

@AlexWaygood
Copy link
Member

I can also repro all your reported speedups from #112728 (comment)!

Co-authored-by: Alex Waygood <[email protected]>
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

LGTM

@barneygale
Copy link
Contributor Author

Thanks tons!

@barneygale barneygale enabled auto-merge (squash) December 4, 2023 23:13
@barneygale barneygale merged commit 304a1b3 into python:main Dec 4, 2023
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
Use `_from_parsed_parts()` to create a pre-joined/pre-parsed path, rather
than passing multiple arguments to `with_segments()`

Co-authored-by: Alex Waygood <[email protected]>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
Use `_from_parsed_parts()` to create a pre-joined/pre-parsed path, rather
than passing multiple arguments to `with_segments()`

Co-authored-by: Alex Waygood <[email protected]>
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