1 from __future__
import annotations
15 for cl
in range(nclasses):
16 decl += f
"class cl{cl:03};\n"
19 for cl
in range(nclasses):
20 decl += f
"class {cl:03} {{\n"
22 bindings += f
' py::class_<cl{cl:03}>(m, "cl{cl:03}")\n'
23 for fn
in range(nfns):
24 ret = random.randint(0, nclasses - 1)
25 params = [random.randint(0, nclasses - 1)
for i
in range(nargs)]
26 decl += f
" cl{ret:03} *fn_{fn:03}("
27 decl +=
", ".join(f
"cl{p:03} *" for p
in params)
29 bindings += f
' .def("fn_{fn:03}", &cl{cl:03}::fn_{fn:03})\n'
33 result =
"#include <pybind11/pybind11.h>\n\n"
34 result +=
"namespace py = pybind11;\n\n"
36 result +=
"PYBIND11_MODULE(example, m) {\n"
46 for cl
in range(nclasses):
47 decl += f
"class cl{cl:03};\n"
50 for cl
in range(nclasses):
51 decl +=
"class cl%03i {\n" % cl
53 bindings += f
' py::class_<cl{cl:03}>("cl{cl:03}")\n'
54 for fn
in range(nfns):
55 ret = random.randint(0, nclasses - 1)
56 params = [random.randint(0, nclasses - 1)
for i
in range(nargs)]
57 decl += f
" cl{ret:03} *fn_{fn:03}("
58 decl +=
", ".join(f
"cl{p:03} *" for p
in params)
60 bindings += f
' .def("fn_{fn:03}", &cl{cl:03}::fn_{fn:03}, py::return_value_policy<py::manage_new_object>())\n'
64 result =
"#include <boost/python.hpp>\n\n"
65 result +=
"namespace py = boost::python;\n\n"
67 result +=
"BOOST_PYTHON_MODULE(example) {\n"
73 for codegen
in [generate_dummy_code_pybind11, generate_dummy_code_boost]:
77 with open(
"test.cpp",
"w")
as f:
78 f.write(codegen(nclasses))
79 n1 = dt.datetime.now()
81 "g++ -Os -shared -rdynamic -undefined dynamic_lookup "
82 "-fvisibility=hidden -std=c++14 test.cpp -I include "
83 "-I /System/Library/Frameworks/Python.framework/Headers -o test.so"
85 n2 = dt.datetime.now()
86 elapsed = (n2 - n1).total_seconds()
87 size = os.stat(
"test.so").st_size
88 print(
" {%i, %f, %i}," % (nclasses * nfns, elapsed, size))