Skip to content

Commit 485619f

Browse files
committed
gh-158585 [WIP] Add _testcapi.bench_writer()
1 parent 9a15440 commit 485619f

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎Modules/_testcapimodule.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,37 @@ uptime_bsd(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
28892889
#endif
28902890

28912891

2892+
static PyObject *
2893+
bench_writer(PyObject *self, PyObject *args)
2894+
{
2895+
Py_ssize_t size, loops;
2896+
if (!PyArg_ParseTuple(args, "nn", &size, &loops)) {
2897+
return NULL;
2898+
}
2899+
2900+
PyTime_t t1, t2;
2901+
(void)PyTime_PerfCounterRaw(&t1);
2902+
for (Py_ssize_t i=0; i < loops; i++) {
2903+
PyBytesWriter *writer = PyBytesWriter_Create(size);
2904+
if (writer == NULL) {
2905+
return NULL;
2906+
}
2907+
2908+
char *str = PyBytesWriter_GetData(writer);
2909+
memset(str, 'x', size);
2910+
2911+
PyObject *bytes = PyBytesWriter_Finish(writer);
2912+
if (bytes == NULL) {
2913+
return NULL;
2914+
}
2915+
Py_DECREF(bytes);
2916+
}
2917+
(void)PyTime_PerfCounterRaw(&t2);
2918+
2919+
return PyFloat_FromDouble(PyTime_AsSecondsDouble(t2 - t1));
2920+
}
2921+
2922+
28922923
static PyMethodDef TestMethods[] = {
28932924
{"set_errno", set_errno, METH_VARARGS},
28942925
{"test_config", test_config, METH_NOARGS},
@@ -2988,6 +3019,7 @@ static PyMethodDef TestMethods[] = {
29883019
#ifdef HAVE_SYSCTLBYNAME
29893020
{"uptime_bsd", uptime_bsd, METH_NOARGS},
29903021
#endif
3022+
{"bench_writer", bench_writer, METH_VARARGS},
29913023
{NULL, NULL} /* sentinel */
29923024
};
29933025

‎bench_writer.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pyperf, _testcapi, functools
2+
runner = pyperf.Runner()
3+
bench = lambda n: functools.partial(_testcapi.bench_writer, n)
4+
#for n in (3, 16, 64, 255, 300):
5+
# runner.bench_time_func(f'bench({n})', bench(n))
6+
runner.bench_time_func(f'bench(3)', bench(3))

0 commit comments

Comments
 (0)