GH-111429: Speed up pathlib.PurePath.[is_]relative_to()#111431
Merged
barneygale merged 4 commits intopython:mainfrom Nov 12, 2023
Merged
GH-111429: Speed up pathlib.PurePath.[is_]relative_to()#111431barneygale merged 4 commits intopython:mainfrom
pathlib.PurePath.[is_]relative_to()#111431barneygale merged 4 commits intopython:mainfrom
Conversation
Contributor
|
Thanks for the change! Overall LGTM. A small suggestion would be - would you mind doing a quick profiling of these 2 functions before/after the change please, just so that we can better understand the effect of the change? |
Contributor
Author
|
The improvement depends on the type of the argument, number of segments in each path, and in the case of $ ./python -m timeit \
-s 'from pathlib import Path; p0 = Path("foo/bar"); p1 = Path("foo")' \
'str(p0.relative_to(p1))'
10000 loops, best of 5: 20.3 usec per loop # before
50000 loops, best of 5: 9.13 usec per loop # after
$ ./python -m timeit \
-s 'from pathlib import Path; p0 = Path("foo/bar"); p1 = "foo"' \
'str(p0.relative_to(p1))'
10000 loops, best of 5: 20.4 usec per loop # before
20000 loops, best of 5: 14.5 usec per loop # after
$ ./python -m timeit \
-s 'from pathlib import Path; p0 = Path("foo/bar"); p1 = Path("foo")' \
'p0.is_relative_to(p1)'
50000 loops, best of 5: 9.01 usec per loop # before
50000 loops, best of 5: 4.15 usec per loop # after
$ ./python -m timeit \
-s 'from pathlib import Path; p0 = Path("foo/bar"); p1 = "foo"' \
'p0.is_relative_to(p1)'
50000 loops, best of 5: 9.04 usec per loop # before
50000 loops, best of 5: 8.91 usec per loop # after |
Contributor
|
@pitrou Sorry for tagging, but based on contributor history, would you mind giving a quick review? |
Contributor
Author
|
Thanks for reviewing @Jason-Y-Z! |
aisk
pushed a commit
to aisk/cpython
that referenced
this pull request
Feb 11, 2024
Glyphack
pushed a commit
to Glyphack/cpython
that referenced
this pull request
Sep 2, 2024
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Avoid unnecessary calls to
with_segments(). This makes bothis_relative_to()andrelative_to()faster when passed aPurePathobject, and makesrelative_to()faster when passed another kind of path-like object (like astr).Also, use
_from_parsed_parts()inrelative_to()to return a pre-parsed path. Operations likestr(p.relative_to(q))are faster as a result.pathlib.PurePath.[is_]relative_to()#111429