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.

90 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_PLUGIN(example) {\n"
  31. result += " py::module m(\"example\");"
  32. result += bindings
  33. result += " return m.ptr();"
  34. result += "}"
  35. return result
  36. def generate_dummy_code_boost(nclasses=10):
  37. decl = ""
  38. bindings = ""
  39. for cl in range(nclasses):
  40. decl += "class cl%03i;\n" % cl
  41. decl += '\n'
  42. for cl in range(nclasses):
  43. decl += "class cl%03i {\n" % cl
  44. decl += "public:\n"
  45. bindings += ' py::class_<cl%03i>("cl%03i")\n' % (cl, cl)
  46. for fn in range(nfns):
  47. ret = random.randint(0, nclasses - 1)
  48. params = [random.randint(0, nclasses - 1) for i in range(nargs)]
  49. decl += " cl%03i *fn_%03i(" % (ret, fn)
  50. decl += ", ".join("cl%03i *" % p for p in params)
  51. decl += ");\n"
  52. bindings += ' .def("fn_%03i", &cl%03i::fn_%03i, py::return_value_policy<py::manage_new_object>())\n' % \
  53. (fn, cl, fn)
  54. decl += "};\n\n"
  55. bindings += ' ;\n'
  56. result = "#include <boost/python.hpp>\n\n"
  57. result += "namespace py = boost::python;\n\n"
  58. result += decl + '\n'
  59. result += "BOOST_PYTHON_MODULE(example) {\n"
  60. result += bindings
  61. result += "}"
  62. return result
  63. for codegen in [generate_dummy_code_pybind11, generate_dummy_code_boost]:
  64. print ("{")
  65. for i in range(0, 10):
  66. nclasses = 2 ** i
  67. with open("test.cpp", "w") as f:
  68. f.write(codegen(nclasses))
  69. n1 = dt.datetime.now()
  70. os.system("g++ -Os -shared -rdynamic -undefined dynamic_lookup "
  71. "-fvisibility=hidden -std=c++14 test.cpp -I include "
  72. "-I /System/Library/Frameworks/Python.framework/Headers -o test.so")
  73. n2 = dt.datetime.now()
  74. elapsed = (n2 - n1).total_seconds()
  75. size = os.stat('test.so').st_size
  76. print(" {%i, %f, %i}," % (nclasses * nfns, elapsed, size))
  77. print ("}")