1

I'm trying to optimize some python code by isolating part of the code into a C++ extension. However, since the code is heavily object-oriented, I'll need to set some attributes as PyObject*. My question is how fast is this compared to pure python code? for example

PyObject* MyClass::doStuff(){
    // _a is a PyObject*
    PyObject* py_attribute = PyObject_GetAttrString(this->_a, "py_attribute");
    PyObject* result = PyObject_CallMethod(py_attribute, "do_stuff");
    return result;
}

vs.

def do_stuff(self):
    result = self._a.py_attribute.do_stuff()
    return result

I've heard that there's an overhead when doing this, but how fast is it compared to pure python?

Thanks

qwerty_99
  • 640
  • 5
  • 20
  • How much of the work is in `do_stuff`? If `do_stuff` is intensive and you haven't changed it then you'll likely see almost no change – DavidW Jun 22 '20 at 17:59
  • it creates a deque or list object appends 1000 numbers to it – qwerty_99 Jun 22 '20 at 18:30
  • `PyObject_GetAttrString` and `PyObject_CallMethod` create new strings everytime, You can avoid this but the necessary changes will ruin maintainability a lot gaining a little bit of performance improvement. To speed it up even more you could use `_PyDict_GetItemId` on the internal dict objects getting 30+ lines of code for almost nothing. to answer the question the strings are created only once by the python compiler and stored within the function for later use. – Szabolcs Dombi Jun 23 '20 at 07:10

0 Answers0