benchmark.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import random
3 import os
4 import time
5 import datetime as dt
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 += "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'
37  result += "PYBIND11_MODULE(example, m) {\n"
38  result += bindings
39  result += "}"
40  return result
41 
42 
43 def 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 
75 for 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 "
83  "-fvisibility=hidden -std=c++14 test.cpp -I include "
84  "-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 ("}")
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
def generate_dummy_code_pybind11(nclasses=10)
Definition: benchmark.py:11
def generate_dummy_code_boost(nclasses=10)
Definition: benchmark.py:43


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:42