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

Speed-up math.dist() by 30% #9628

Merged
merged 2 commits into from
Sep 29, 2018
Merged

Conversation

rhettinger
Copy link
Contributor

Manual type checking is much faster than the argument clinic equivalent.

--------- baseline ------------
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 95.5 nsec per loop
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 95.3 nsec per loop
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 95.5 nsec per loop

------ patched ------
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 67 nsec per loop
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 67.2 nsec per loop
$ pytime -r11 -s 'from math import dist' -s 'p=(1.1, 2.2)' -s 'q=(1.5, 2.5)' 'dist(p, q)'
5000000 loops, best of 11: 67.3 nsec per loop

@@ -2126,6 +2126,15 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
double diffs_on_stack[NUM_STACK_ELEMS];
double *diffs = diffs_on_stack;

if (!PyTuple_Check(p)) {
Copy link
Member

Choose a reason for hiding this comment

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

Two ifs can be merged since their bodies are same.

@serhiy-storchaka serhiy-storchaka removed their assignment Sep 28, 2018
@rhettinger rhettinger merged commit df81015 into python:master Sep 29, 2018
@rhettinger rhettinger deleted the math-fast-dist branch September 29, 2018 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage skip issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants