You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.9 KiB

  1. import random
  2. import os
  3. import time
  4. import datetime as dt
  5. nfns = 4 # Functions per class
  6. nargs = 4 # Arguments per function
  7. def generate_dummy_code_pybind11(nclasses=10):
  8. decl = ""
  9. bindings = ""
  10. for cl in range(nclasses):
  11. decl += "class cl%03i;\n" % cl
  12. decl += '\n'
  13. for cl in range(nclasses):
  14. decl += "class cl%03i {\n" % cl
  15. decl += "public:\n"
  16. bindings += ' py::class_<cl%03i>(m, "cl%03i")\n' % (cl, cl)
  17. for fn in range(nfns):
  18. ret = random.randint(0, nclasses - 1)
  19. params = [random.randint(0, nclasses - 1) for i in range(nargs)]
  20. decl += " cl%03i *fn_%03i(" % (ret, fn)
  21. decl += ", ".join("cl%03i *" % p for p in params)
  22. decl += ");\n"
  23. bindings += ' .def("fn_%03i", &cl%03i::fn_%03i)\n' % \
  24. (fn, cl, fn)
  25. decl += "};\n\n"
  26. bindings += ' ;\n'
  27. result = "#include <pybind11/pybind11.h>\n\n"
  28. result += "namespace py = pybind11;\n\n"
  29. result += decl + '\n'
  30. result += "PYBIND11_MODULE(example, m) {\n"
  31. result += bindings
  32. result += "}"
  33. return result
  34. def generate_dummy_code_boost(nclasses=10):
  35. decl = ""
  36. bindings = ""
  37. for cl in range(nclasses):
  38. decl += "class cl%03i;\n" % cl
  39. decl += '\n'
  40. for cl in range(nclasses):
  41. decl += "class cl%03i {\n" % cl
  42. decl += "public:\n"
  43. bindings += ' py::class_<cl%03i>("cl%03i")\n' % (cl, cl)
  44. for fn in range(nfns):
  45. ret = random.randint(0, nclasses - 1)
  46. params = [random.randint(0, nclasses - 1) for i in range(nargs)]
  47. decl += " cl%03i *fn_%03i(" % (ret, fn)
  48. decl += ", ".join("cl%03i *" % p for p in params)
  49. decl += ");\n"
  50. bindings += ' .def("fn_%03i", &cl%03i::fn_%03i, py::return_value_policy<py::manage_new_object>())\n' % \
  51. (fn, cl, fn)
  52. decl += "};\n\n"
  53. bindings += ' ;\n'
  54. result = "#include <boost/python.hpp>\n\n"
  55. result += "namespace py = boost::python;\n\n"
  56. result += decl + '\n'
  57. result += "BOOST_PYTHON_MODULE(example) {\n"
  58. result += bindings
  59. result += "}"
  60. return result
  61. for codegen in [generate_dummy_code_pybind11, generate_dummy_code_boost]:
  62. print ("{")
  63. for i in range(0, 10):
  64. nclasses = 2 ** i
  65. with open("test.cpp", "w") as f:
  66. f.write(codegen(nclasses))
  67. n1 = dt.datetime.now()
  68. os.system("g++ -Os -shared -rdynamic -undefined dynamic_lookup "
  69. "-fvisibility=hidden -std=c++14 test.cpp -I include "
  70. "-I /System/Library/Frameworks/Python.framework/Headers -o test.so")
  71. n2 = dt.datetime.now()
  72. elapsed = (n2 - n1).total_seconds()
  73. size = os.stat('test.so').st_size
  74. print(" {%i, %f, %i}," % (nclasses * nfns, elapsed, size))
  75. print ("}")