Skip to content

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

Merged
rhettinger merged 2 commits into
python:masterfrom
rhettinger:math-fast-dist
Sep 29, 2018
Merged

Speed-up math.dist() by 30%#9628
rhettinger merged 2 commits into
python:masterfrom
rhettinger:math-fast-dist

Conversation

@rhettinger

Copy link
Copy Markdown
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

Comment thread Modules/mathmodule.c Outdated
double diffs_on_stack[NUM_STACK_ELEMS];
double *diffs = diffs_on_stack;

if (!PyTuple_Check(p)) {

Copy link
Copy Markdown
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