Ctypes vs regular Python: Performance comparison

preview_player
Показать описание
Python ctypes is a foreign function library that allows Python code to call C functions directly. This can be useful for improving the performance of Python code, particularly when working with large data sets or computationally intensive tasks.

But is it faster than regular Python code? Is it faster under all circumstances, or only a few? Let's find out in today's performance comparison video.

Website Link (code available)

Related Links:
Рекомендации по теме
Комментарии
Автор

I was also testing recursive Fibonacci function and ctypes provides faster results there as well. The results for Fibonacci(30):
Python: 140 ms
Ctypes: 5 ms
Pure C: 3.8-4 ms

nikolaydemidov
Автор

You should look at Numba and it's jit compiler, in my experience it's extremely fast, but is much easier to use in a normal python project than some of the other options.

For example I just did your test with fib, comparing Numba to raw python. Same code, just one with the @njit() decorator. Using timeit to time fib(200), python was 7.8us, Numba was 226ns.

copperz
Автор

Hi I am writing a gui library based on win32 api in Python using ctypes. It's faster than I thought. But my experiments with cython was a failure. I think Cython took away so many nice python features. I can't even inherit from a class which I imported from another module. That's ridiculous. They made python faster with cython, but they tear off so many nice features also.

kcvinu
welcome to shbcf.ru