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

3
  • 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 Commented Jun 22, 2020 at 17:59
  • it creates a deque or list object appends 1000 numbers to it Commented Jun 22, 2020 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. Commented Jun 23, 2020 at 7:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.