diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index d5616fd59c..6ddfd0b1d4 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -365,6 +365,31 @@ test_edit_cost(PyObject *self, PyObject *Py_UNUSED(args)) } +static PyObject * +test_bench(PyObject *self, PyObject *args) +{ + PyObject *func, *arg; + Py_ssize_t nloops; + _PyTime_t t1, t2; + if (!PyArg_ParseTuple(args, "nOO", &nloops, &func, &arg)) { + return NULL; + } + + t1 = _PyTime_GetPerfCounter(); + for (size_t i=0; i<(size_t)nloops; i++) { + PyObject *res = PyObject_CallOneArg(func, arg); + if (res == NULL) { + return res; + } + Py_DECREF(res); + } + t2 = _PyTime_GetPerfCounter(); + + double d = _PyTime_AsSecondsDouble(t2 - t1); + return PyFloat_FromDouble(d); +} + + static PyMethodDef TestMethods[] = { {"get_configs", get_configs, METH_NOARGS}, {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, @@ -376,6 +401,7 @@ static PyMethodDef TestMethods[] = { {"set_config", test_set_config, METH_O}, {"test_atomic_funcs", test_atomic_funcs, METH_NOARGS}, {"test_edit_cost", test_edit_cost, METH_NOARGS}, + {"test_bench", test_bench, METH_VARARGS}, {NULL, NULL} /* sentinel */ };