Skip to content

gh-117639: Special case for "" & "." in posixpath.abspath() #117640

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

Closed
wants to merge 3 commits into from

Conversation

nineteendo
Copy link
Contributor

@nineteendo nineteendo commented Apr 8, 2024

Benchmark:

# test.sh
echo "abspath()" && python -m timeit -s "import before.posixpath" "before.posixpath.abspath('foo')" && python -m timeit -s "import after.posixpath" "after.posixpath.abspath('foo')"
echo "abspath('.')" && python -m timeit -s "import before.posixpath" "before.posixpath.abspath('.')" && python -m timeit -s "import after.posixpath" "after.posixpath.abspath('.')"
echo "relpath()" && python -m timeit -s "import before.posixpath" "before.posixpath.relpath('foo', 'bar')" && python -m timeit -s "import after.posixpath" "after.posixpath.relpath('foo', 'bar')"
echo "relpath('foo')" && python -m timeit -s "import before.posixpath" "before.posixpath.relpath('foo')" && python -m timeit -s "import after.posixpath" "after.posixpath.relpath('foo')"
echo "relpath('/')" && python -m timeit -s "import before.posixpath" "before.posixpath.relpath('/')" && python -m timeit -s "import after.posixpath" "after.posixpath.relpath('/')"
abspath()
20000 loops, best of 5: 17.7 usec per loop # before
20000 loops, best of 5: 17.4 usec per loop # after
# -> 1.02x faster
abspath('.')
20000 loops, best of 5: 18.1 usec per loop # before
20000 loops, best of 5: 15.7 usec per loop # after
# -> 1.15x faster (for '' & '.')
relpath()
10000 loops, best of 5: 39.4 usec per loop # before
10000 loops, best of 5: 39.8 usec per loop # after
# -> no difference
relpath('foo')
5000 loops, best of 5: 38.9 usec per loop # before
10000 loops, best of 5: 37.4 usec per loop # after
# -> 1.04x faster (for a single relative path)
relpath('/')
10000 loops, best of 5: 22 usec per loop # before
10000 loops, best of 5: 20.7 usec per loop # after
# -> 1.06x faster (for a single absolute path)

@nineteendo nineteendo marked this pull request as ready for review April 8, 2024 15:05
@nineteendo nineteendo changed the title gh-117639: Special case for current dir in posixpath.abspath() gh-117639: Special case for "" & "." in posixpath.abspath() Apr 9, 2024
@nineteendo nineteendo closed this Apr 9, 2024
@nineteendo nineteendo deleted the speedup-posixpath.abspath branch April 9, 2024 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants