The difference between a C++ and Python API can be massive, but it also depends on the quality of the Python implementation.
A key factor to consider when setting expectations, is the way the API is used to communicate information. There are two main ways to interact with APIs: (i) reading files, and (ii) creating models in the API.
For example, our solver's Python API allows people to read a model like so:
For open source formats, such as .nl or .mps, this command is a thin wrapper around the C++ library which reads the file directly.
For our AMPL and GAMS interfaces, the Python command is a thin wrapper around their respective C++ APIs.
In both cases, the Python overhead is minimal, because we invoke the C++ libraries directly.
However, many solvers (especially open-source) will first construct the problem in Python, and then copy/pipe that object to the solver, which can be hundreds of times slower.
This is trickier. Models are typically in some condensed format (like .nl), or are parsed by a dedicated parser (like AMPL) which transforms them to condensed format, so passing them to a solver quickly is doable.
However, setting up the math through an API can't be as effective, unless a condensed format is used (which takes us back to the "Reading files" case).
Say I want to add a non-linear constraint. In Octeract Engine, the binding in Python is:
Because we have a high performance parser built into the engine, we can parse the string directly, extract the variables automatically, create the computational graph, and a name for the constraint. The only overhead here is copying the string from Python to C++.
In C++ however, we also allow users to create constraints directly from a syntax tree or computational graph, which is much faster.
This is an important point: it's not that we can't expose the graph methods to Python for speed - we could, but forcing a user to build a complex object like that in Python defeats the purpose of using Python in the first place.
As long as the structures we pass to an API are high performance structures, such as a condensed file format, a sparse matrix, or a computational graph, there should be little difference between C++ and Python, as long as the Python implementation is good.
If we are doing incremental changes to the model through the API, or building it piece by piece, the Python API can be considerably slower.