benchmark.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import datetime as dt
4 import os
5 import random
6 
7 nfns = 4 # Functions per class
8 nargs = 4 # Arguments per function
9 
10 
12  decl = ""
13  bindings = ""
14 
15  for cl in range(nclasses):
16  decl += f"class cl{cl:03};\n"
17  decl += "\n"
18 
19  for cl in range(nclasses):
20  decl += f"class {cl:03} {{\n"
21  decl += "public:\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)
28  decl += ");\n"
29  bindings += f' .def("fn_{fn:03}", &cl{cl:03}::fn_{fn:03})\n'
30  decl += "};\n\n"
31  bindings += " ;\n"
32 
33  result = "#include <pybind11/pybind11.h>\n\n"
34  result += "namespace py = pybind11;\n\n"
35  result += decl + "\n"
36  result += "PYBIND11_MODULE(example, m) {\n"
37  result += bindings
38  result += "}"
39  return result
40 
41 
42 def generate_dummy_code_boost(nclasses=10):
43  decl = ""
44  bindings = ""
45 
46  for cl in range(nclasses):
47  decl += f"class cl{cl:03};\n"
48  decl += "\n"
49 
50  for cl in range(nclasses):
51  decl += "class cl%03i {\n" % cl
52  decl += "public:\n"
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)
59  decl += ");\n"
60  bindings += f' .def("fn_{fn:03}", &cl{cl:03}::fn_{fn:03}, py::return_value_policy<py::manage_new_object>())\n'
61  decl += "};\n\n"
62  bindings += " ;\n"
63 
64  result = "#include <boost/python.hpp>\n\n"
65  result += "namespace py = boost::python;\n\n"
66  result += decl + "\n"
67  result += "BOOST_PYTHON_MODULE(example) {\n"
68  result += bindings
69  result += "}"
70  return result
71 
72 
73 for codegen in [generate_dummy_code_pybind11, generate_dummy_code_boost]:
74  print("{")
75  for i in range(10):
76  nclasses = 2**i
77  with open("test.cpp", "w") as f:
78  f.write(codegen(nclasses))
79  n1 = dt.datetime.now()
80  os.system(
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"
84  )
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("}")
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
benchmark.generate_dummy_code_boost
def generate_dummy_code_boost(nclasses=10)
Definition: benchmark.py:42
benchmark.generate_dummy_code_pybind11
def generate_dummy_code_pybind11(nclasses=10)
Definition: benchmark.py:11
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:01:54