diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index d5616fd59c..52276b0886 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -14,6 +14,7 @@ #include "Python.h" #include "pycore_atomic_funcs.h" // _Py_atomic_int_get() #include "pycore_bitutils.h" // _Py_bswap32() +#include "pycore_call.h" // _PyObject_CallNoArgs() #include "pycore_gc.h" // PyGC_Head #include "pycore_hashtable.h" // _Py_hashtable_new() #include "pycore_initconfig.h" // _Py_GetConfigsAsDict() @@ -365,6 +366,63 @@ test_edit_cost(PyObject *self, PyObject *Py_UNUSED(args)) } +static PyObject * +test_noop(PyObject *self, PyObject *Py_UNUSED(args)) +{ + Py_RETURN_NONE; +} + + +static PyObject * +test_bench_public(PyObject *self, PyObject *args) +{ + PyObject *func; + Py_ssize_t nloops; + _PyTime_t t1, t2; + if (!PyArg_ParseTuple(args, "nO", &nloops, &func)) { + return NULL; + } + + t1 = _PyTime_GetPerfCounter(); + for (size_t i=0; i<(size_t)nloops; i++) { + PyObject *res = PyObject_CallNoArgs(func); + if (res == NULL) { + return res; + } + Py_DECREF(res); + } + t2 = _PyTime_GetPerfCounter(); + + double d = _PyTime_AsSecondsDouble(t2 - t1); + return PyFloat_FromDouble(d); +} + + +static PyObject * +test_bench_inline(PyObject *self, PyObject *args) +{ + PyObject *func; + Py_ssize_t nloops; + _PyTime_t t1, t2; + if (!PyArg_ParseTuple(args, "nO", &nloops, &func)) { + return NULL; + } + + t1 = _PyTime_GetPerfCounter(); + for (size_t i=0; i<(size_t)nloops; i++) { + PyObject *res = _PyObject_CallNoArgs(func); + 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 +434,9 @@ 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}, + {"noop", test_noop, METH_NOARGS}, + {"test_bench_public", test_bench_public, METH_VARARGS}, + {"test_bench_inline", test_bench_inline, METH_VARARGS}, {NULL, NULL} /* sentinel */ };