diff --git a/Python/sysmodule.c b/Python/sysmodule.c index ae3cbf1954..93e8e98656 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1970,6 +1970,32 @@ sys_getandroidapilevel_impl(PyObject *module) } #endif /* ANDROID_API_LEVEL */ +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 sys_methods[] = { /* Might as well keep this in alphabetic order */ SYS_ADDAUDITHOOK_METHODDEF @@ -2023,6 +2049,7 @@ static PyMethodDef sys_methods[] = { SYS_GET_ASYNCGEN_HOOKS_METHODDEF SYS_GETANDROIDAPILEVEL_METHODDEF SYS_UNRAISABLEHOOK_METHODDEF + {"test_bench", test_bench, METH_VARARGS}, {NULL, NULL} /* sentinel */ };