Question

Is there any data that visualizes just how much performance can be gained by using the Python C API when writing functions directly in C to be used as python modules?

Besides the obvious fact that "C is faster"; is there any data that compares Python C API vs C?

Was it helpful?

Solution

I'm not sure there is any easy way to get such "data". It really depends on what you are doing, and you have to take into account that transferring the data from the Python side to the C side and back again will be an extra load on the system, compared to simply perform the operations in Python directly.

If you are doing a lot of calculations, and those calculations are complicated but can't be done in an existing library (such as "numpy"), then it may be worth doing. And of course, calculation doesn't necessarily have to be "mathematics", it could be shuffling data in a large array, or making if (x > y) z++; type operations. But you really need to have a large amount of stuff to do before it makes sense to convert the data from "python" to "C" type of data, and back again.

It's a bit like asking "How much faster is this sporty car than that not-so-sporty car", and if you drive in a big city with lots of congestion, the difference may not be any at all - but if you take them to a race-track, where the sporty car gets to stretch its legs properly, the winner is quite obvious.

In the "car" theme, the "congestion" is lots of calls to a very small function in C, that doesn't do much work - the "convert from Python to C and back to Python data" will be the congestion/traffic lights. If you have large lumps of data, then you get a bit more "race-track" effect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top