blob: 023477212ee3ca34353067b196e9959144444f33 [file] [log] [blame]
Henry Schreinerd8c7ee02020-07-20 13:35:21 -04001# -*- coding: utf-8 -*-
Wenzel Jakob5cd33112015-10-20 00:58:59 +02002import random
3import os
4import time
5import datetime as dt
6
7nfns = 4 # Functions per class
8nargs = 4 # Arguments per function
9
10
11def generate_dummy_code_pybind11(nclasses=10):
12 decl = ""
13 bindings = ""
14
15 for cl in range(nclasses):
16 decl += "class cl%03i;\n" % cl
17 decl += '\n'
18
19 for cl in range(nclasses):
20 decl += "class cl%03i {\n" % cl
21 decl += "public:\n"
22 bindings += ' py::class_<cl%03i>(m, "cl%03i")\n' % (cl, cl)
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 += " cl%03i *fn_%03i(" % (ret, fn)
27 decl += ", ".join("cl%03i *" % p for p in params)
28 decl += ");\n"
29 bindings += ' .def("fn_%03i", &cl%03i::fn_%03i)\n' % \
30 (fn, cl, fn)
31 decl += "};\n\n"
32 bindings += ' ;\n'
33
34 result = "#include <pybind11/pybind11.h>\n\n"
35 result += "namespace py = pybind11;\n\n"
36 result += decl + '\n'
Dean Moldovan443ab592017-04-24 01:51:44 +020037 result += "PYBIND11_MODULE(example, m) {\n"
Wenzel Jakob5cd33112015-10-20 00:58:59 +020038 result += bindings
Wenzel Jakob5cd33112015-10-20 00:58:59 +020039 result += "}"
40 return result
41
42
43def generate_dummy_code_boost(nclasses=10):
44 decl = ""
45 bindings = ""
46
47 for cl in range(nclasses):
48 decl += "class cl%03i;\n" % cl
49 decl += '\n'
50
51 for cl in range(nclasses):
52 decl += "class cl%03i {\n" % cl
53 decl += "public:\n"
54 bindings += ' py::class_<cl%03i>("cl%03i")\n' % (cl, cl)
55 for fn in range(nfns):
56 ret = random.randint(0, nclasses - 1)
57 params = [random.randint(0, nclasses - 1) for i in range(nargs)]
58 decl += " cl%03i *fn_%03i(" % (ret, fn)
59 decl += ", ".join("cl%03i *" % p for p in params)
60 decl += ");\n"
61 bindings += ' .def("fn_%03i", &cl%03i::fn_%03i, py::return_value_policy<py::manage_new_object>())\n' % \
62 (fn, cl, fn)
63 decl += "};\n\n"
64 bindings += ' ;\n'
65
66 result = "#include <boost/python.hpp>\n\n"
67 result += "namespace py = boost::python;\n\n"
68 result += decl + '\n'
69 result += "BOOST_PYTHON_MODULE(example) {\n"
70 result += bindings
71 result += "}"
72 return result
73
74
75for codegen in [generate_dummy_code_pybind11, generate_dummy_code_boost]:
76 print ("{")
77 for i in range(0, 10):
78 nclasses = 2 ** i
79 with open("test.cpp", "w") as f:
80 f.write(codegen(nclasses))
81 n1 = dt.datetime.now()
82 os.system("g++ -Os -shared -rdynamic -undefined dynamic_lookup "
Wenzel Jakob66c9a402016-01-17 22:36:36 +010083 "-fvisibility=hidden -std=c++14 test.cpp -I include "
Wenzel Jakob5cd33112015-10-20 00:58:59 +020084 "-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))
89 print ("}")