Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2889,6 +2889,37 @@ uptime_bsd(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
#endif


static PyObject *
bench_writer(PyObject *self, PyObject *args)
{
Py_ssize_t size, loops;
if (!PyArg_ParseTuple(args, "nn", &size, &loops)) {
return NULL;
}

PyTime_t t1, t2;
(void)PyTime_PerfCounterRaw(&t1);
for (Py_ssize_t i=0; i < loops; i++) {
PyBytesWriter *writer = PyBytesWriter_Create(size);
if (writer == NULL) {
return NULL;
}

char *str = PyBytesWriter_GetData(writer);
memset(str, 'x', size);

PyObject *bytes = PyBytesWriter_Finish(writer);
if (bytes == NULL) {
return NULL;
}
Py_DECREF(bytes);
}
(void)PyTime_PerfCounterRaw(&t2);

return PyFloat_FromDouble(PyTime_AsSecondsDouble(t2 - t1));
}


static PyMethodDef TestMethods[] = {
{"set_errno", set_errno, METH_VARARGS},
{"test_config", test_config, METH_NOARGS},
Expand Down Expand Up @@ -2988,6 +3019,7 @@ static PyMethodDef TestMethods[] = {
#ifdef HAVE_SYSCTLBYNAME
{"uptime_bsd", uptime_bsd, METH_NOARGS},
#endif
{"bench_writer", bench_writer, METH_VARARGS},
{NULL, NULL} /* sentinel */
};

Expand Down
6 changes: 6 additions & 0 deletions bench_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pyperf, _testcapi, functools
runner = pyperf.Runner()
bench = lambda n: functools.partial(_testcapi.bench_writer, n)
#for n in (3, 16, 64, 255, 300):
# runner.bench_time_func(f'bench({n})', bench(n))
runner.bench_time_func(f'bench(3)', bench(3))
Loading