diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 101d54932d..f0063dce80 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -5263,6 +5263,58 @@ pynumber_tobase(PyObject *module, PyObject *args) } +_Py_IDENTIFIER(a); +_Py_IDENTIFIER(abc); + +static PyObject* +bench_fromid1(PyObject *module, PyObject *args) +{ + Py_ssize_t loops; + if (!PyArg_ParseTuple(args, "n", &loops)) { + return NULL; + } + _PyTime_t t1 = _PyTime_GetPerfCounter(); + for (Py_ssize_t i=0; i < loops; i++) { + _PyUnicode_FromId(&PyId_a); + _PyUnicode_FromId(&PyId_a); + _PyUnicode_FromId(&PyId_a); + _PyUnicode_FromId(&PyId_a); + _PyUnicode_FromId(&PyId_a); + _PyUnicode_FromId(&PyId_a); + _PyUnicode_FromId(&PyId_a); + _PyUnicode_FromId(&PyId_a); + _PyUnicode_FromId(&PyId_a); + _PyUnicode_FromId(&PyId_a); + } + _PyTime_t t2 = _PyTime_GetPerfCounter(); + return PyFloat_FromDouble(_PyTime_AsSecondsDouble(t2 - t1)); +} + +static PyObject* +bench_fromid2(PyObject *module, PyObject *args) +{ + Py_ssize_t loops; + if (!PyArg_ParseTuple(args, "n", &loops)) { + return NULL; + } + _PyTime_t t1 = _PyTime_GetPerfCounter(); + for (Py_ssize_t i=0; i < loops; i++) { + _PyUnicode_FromId(&PyId_abc); + _PyUnicode_FromId(&PyId_abc); + _PyUnicode_FromId(&PyId_abc); + _PyUnicode_FromId(&PyId_abc); + _PyUnicode_FromId(&PyId_abc); + _PyUnicode_FromId(&PyId_abc); + _PyUnicode_FromId(&PyId_abc); + _PyUnicode_FromId(&PyId_abc); + _PyUnicode_FromId(&PyId_abc); + _PyUnicode_FromId(&PyId_abc); + } + _PyTime_t t2 = _PyTime_GetPerfCounter(); + return PyFloat_FromDouble(_PyTime_AsSecondsDouble(t2 - t1)); +} + + static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *); static PyMethodDef TestMethods[] = { @@ -5523,6 +5575,8 @@ static PyMethodDef TestMethods[] = { {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL}, {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS}, {"pynumber_tobase", pynumber_tobase, METH_VARARGS}, + {"bench_fromid1", bench_fromid1, METH_VARARGS}, + {"bench_fromid2", bench_fromid2, METH_VARARGS}, {NULL, NULL} /* sentinel */ };