Performances issue when using the Python code #4527
Unanswered
luigibrancati
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone,
This is my first time using pybind11 for a project. I have been using it in order to provide a Python interface for a C++ code: any computation happens on the C++ side, while Python only offers some basic classes and functions bound to the corresponding C++ objects. No object are streamed between C++ and Python as far as I can tell, except of course for the inputs and outputs of function calls.
The reason I'm asking for some feedback is because I'm experiencing a performance degradation whose cause I cannot identify. I can't provide a sample code, but I'll try to explain the issue: I have been refactoring an old code which consisted of a monolith function
poker_algo_old::calculate_hand_frequency
, which I split in several smaller helper functions that are called by the new main functionpoker_algo_new::calculate_hand_frequency
. These two functions are both bound in Python ascalculate_hand_frequency
andcalculate_hand_frequency_new
, as shown in themain.cpp
below:main.cpp
Note that only the main functions and classes are bound to Python objects, none of the helper functions mentioned above is bound to Python.
In order to compare the performances on the C++ side of the refactored code vs the previous monolithic code, I compiled both with just an
-O1
flag and usedgprof
: the total running time for 10 runs of the both functions is the same, the refactored code doesn't show any degradation in performances.Old code profiling
Refactored code profiling
Each function takes about 3.30 seconds to run 10 times. However, when I try to run the same functions on Python while monitoring their total running time, I get that the new function takes twice as long as the old one
performances.py
The old function takes about the same time as the C++ code, while the refactored one takes twice as much. I cannot explain the gap between these two functions, since there is no such gap on the C++ side. The refactored function only uses some helper functions which can be seen in the
gprof
output, but these are not even bound to the Python side.Note: I'm aware that streaming object between C++ <--> Python can degrade performances, but all calculations here happen on the C++ side and only the output of the function is streamed to Python at the end of the calculation. Also, both new and old functions have the same inputs and outputs, so this wouldn't explaing the gap in performances.
Beta Was this translation helpful? Give feedback.
All reactions