Skip to content
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

gh-117607: Speedup os.path.relpath() #117608

Merged
merged 9 commits into from
May 1, 2024

Conversation

nineteendo
Copy link
Contributor

@nineteendo nineteendo commented Apr 7, 2024

Lib/ntpath.py Show resolved Hide resolved
@nineteendo nineteendo marked this pull request as ready for review April 8, 2024 11:43
…e-117607.C978BD.rst

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
@nineteendo
Copy link
Contributor Author

@eryksun, could you run a benchmark for os.path.relpath() on your machine? Otherwise it might undersell the performance improvement.

@eryksun
Copy link
Contributor

eryksun commented Apr 17, 2024

@eryksun, could you run a benchmark for os.path.relpath() on your machine? Otherwise it might undersell the performance improvement.

Windows - ntpath - 1.407-1.565 speedup

> .\python_main -m timeit -s "import ntpath" "ntpath.relpath('C:foo', start='C:/Users/account')"
50000 loops, best of 5: 6.7 usec per loop
> .\python_work -m timeit -s "import ntpath" "ntpath.relpath('C:foo', start='C:/Users/account')"
50000 loops, best of 5: 4.28 usec per loop

> .\python_main -m timeit -s "import ntpath" "ntpath.relpath('C:/', start='C:/Users/account')"
50000 loops, best of 5: 4.84 usec per loop
> .\python_work -m timeit -s "import ntpath" "ntpath.relpath('C:/', start='C:/Users/account')"
100000 loops, best of 5: 3.44 usec per loop

Linux - posixpath - 1.137-1.189 speedup

$ ./python_main -m timeit -s "import posixpath" "posixpath.relpath('foo', start='/home/account')"
100000 loops, best of 5: 3.14 usec per loop
$ ./python_work -m timeit -s "import posixpath" "posixpath.relpath('foo', start='/home/account')"
100000 loops, best of 5: 2.64 usec per loop

$ ./python_main -m timeit -s "import posixpath" "posixpath.relpath('/', start='/home/account')"
200000 loops, best of 5: 1.74 usec per loop
$ ./python_work -m timeit -s "import posixpath" "posixpath.relpath('/', start='/home/account')"
200000 loops, best of 5: 1.53 usec per loop

Windows - posixpath - 1.123-1.136 speedup

> .\python_main -m timeit -s "import posixpath" "posixpath.relpath('foo', start='/home/account')"
50000 loops, best of 5: 4.39 usec per loop
> .\python_work -m timeit -s "import posixpath" "posixpath.relpath('foo', start='/home/account')"
100000 loops, best of 5: 3.91 usec per loop

> .\python_main -m timeit -s "import posixpath" "posixpath.relpath('/', start='/home/account')"
100000 loops, best of 5: 3.1 usec per loop
> .\python_work -m timeit -s "import posixpath" "posixpath.relpath('/', start='/home/account')"
100000 loops, best of 5: 2.73 usec per loop

Linux - ntpath - 1.006-1.007 slowdown (no change)

$ ./python_main -m timeit -s "import ntpath" "ntpath.relpath('C:foo', start='C:/Users/account')"
50000 loops, best of 5: 8.63 usec per loop
$ ./python_work -m timeit -s "import ntpath" "ntpath.relpath('C:foo', start='C:/Users/account')"
50000 loops, best of 5: 8.69 usec per loop

$ ./python_main -m timeit -s "import ntpath" "ntpath.relpath('C:/', start='C:/Users/account')"
50000 loops, best of 5: 6.29 usec per loop
$ ./python_work -m timeit -s "import ntpath" "ntpath.relpath('C:/', start='C:/Users/account')"
50000 loops, best of 5: 6.33 usec per loop

@nineteendo
Copy link
Contributor Author

nineteendo commented Apr 17, 2024

Could you also check with start not set or a relative path?

@eryksun
Copy link
Contributor

eryksun commented Apr 17, 2024

Could you also check with start not set or a relative path?

Sure, except testing ntpath.relpath("C:/") isn't meaningful on POSIX. It fails because the "C:" drive is a different device namespace.

Windows - ntpath - 1.201-1.269 speedup

> .\python_main -m timeit -s "import os,ntpath; os.chdir('C:/Users')" "ntpath.relpath('foo')"
50000 loops, best of 5: 4.84 usec per loop
> .\python_work -m timeit -s "import os,ntpath; os.chdir('C:/Users')" "ntpath.relpath('foo')"
50000 loops, best of 5: 4.03 usec per loop

> .\python_main -m timeit -s "import os,ntpath; os.chdir('C:/Users')" "ntpath.relpath('C:/')"
50000 loops, best of 5: 4.11 usec per loop
> .\python_work -m timeit -s "import os,ntpath; os.chdir('C:/Users')" "ntpath.relpath('C:/')"
100000 loops, best of 5: 3.24 usec per loop

Linux - posixpath - 1.309-1.434 speedup

$ ./python_main -m timeit -s "import os,posixpath; os.chdir('/home')" "posixpath.relpath('foo')"
100000 loops, best of 5: 2.94 usec per loop
$ ./python_work -m timeit -s "import os,posixpath; os.chdir('/home')" "posixpath.relpath('foo')"
100000 loops, best of 5: 2.05 usec per loop

$ ./python_main -m timeit -s "import os,posixpath; os.chdir('/home')" "posixpath.relpath('/')"
100000 loops, best of 5: 2.12 usec per loop
$ ./python_work -m timeit -s "import os,posixpath; os.chdir('/home')" "posixpath.relpath('/')"
200000 loops, best of 5: 1.62 usec per loop

Windows - posixpath - 1.060 slowdown, 1.019 speedup

> .\python_main -m timeit -s "import os,posixpath; os.chdir('C:/Users')" "posixpath.relpath('foo')"
50000 loops, best of 5: 4.01 usec per loop
> .\python_work -m timeit -s "import os,posixpath; os.chdir('C:/Users')" "posixpath.relpath('foo')"
50000 loops, best of 5: 4.25 usec per loop

> .\python_main -m timeit -s "import os,posixpath; os.chdir('C:/Users')" "posixpath.relpath('/')"
100000 loops, best of 5: 3.22 usec per loop
> .\python_work -m timeit -s "import os,posixpath; os.chdir('C:/Users')" "posixpath.relpath('/')"
100000 loops, best of 5: 3.16 usec per loop

Linux - ntpath - 1.004 speedup (no change)

$ ./python_main -m timeit -s "import os,ntpath; os.chdir('/home')" "ntpath.relpath('foo')"
50000 loops, best of 5: 8.23 usec per loop
$ ./python_work -m timeit -s "import os,ntpath; os.chdir('/home')" "ntpath.relpath('foo')"
50000 loops, best of 5: 8.2 usec per loop

@erlend-aasland
Copy link
Contributor

Thanks for running benchmarks, @eryksun 🙏

@nineteendo
Copy link
Contributor Author

cc @barneygale, @zooba, @serhiy-storchaka

@zooba zooba merged commit a7711a2 into python:main May 1, 2024
33 checks passed
@zooba
Copy link
Member

zooba commented May 1, 2024

Thanks!

@nineteendo nineteendo deleted the speedup-os.path.relpath branch May 2, 2024 05:41
SonicField pushed a commit to SonicField/cpython that referenced this pull request May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants