Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df81015

Browse files
authored
Speed-up math.dist() by 30% (GH-9628)
1 parent e45473e commit df81015

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

Modules/clinic/mathmodule.c.h

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/mathmodule.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,8 +2101,8 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
21012101
/*[clinic input]
21022102
math.dist
21032103
2104-
p: object(subclass_of='&PyTuple_Type')
2105-
q: object(subclass_of='&PyTuple_Type')
2104+
p: object
2105+
q: object
21062106
/
21072107
21082108
Return the Euclidean distance between two points p and q.
@@ -2116,7 +2116,7 @@ Roughly equivalent to:
21162116

21172117
static PyObject *
21182118
math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
2119-
/*[clinic end generated code: output=56bd9538d06bbcfe input=937122eaa5f19272]*/
2119+
/*[clinic end generated code: output=56bd9538d06bbcfe input=8c83c07c7a524664]*/
21202120
{
21212121
PyObject *item;
21222122
double max = 0.0;
@@ -2126,6 +2126,11 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
21262126
double diffs_on_stack[NUM_STACK_ELEMS];
21272127
double *diffs = diffs_on_stack;
21282128

2129+
if (!PyTuple_Check(p) || !PyTuple_Check(q)) {
2130+
PyErr_SetString(PyExc_TypeError, "dist argument must be a tuple");
2131+
return NULL;
2132+
}
2133+
21292134
m = PyTuple_GET_SIZE(p);
21302135
n = PyTuple_GET_SIZE(q);
21312136
if (m != n) {

0 commit comments

Comments
 (0)