From the course: Faster Python Code

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

C extensions

C extensions - Python Tutorial

From the course: Faster Python Code

Start my 1-month free trial

C extensions

- [Instructor] Sometimes no matter what we try, Python is just not fast enough. What we do then is identify the bottleneck in our program and re-write it an a more performant language. Most of the time this language is C. There are less painful options such as cython. However, c wizards know how to squeeze every cycle out of the CPU. I recommend going dowm the C extension path only after you tried other options and if you're familiar with C. A lot of times, changing algorithms at the Python level will yield much better results than rewriting C. The python C API is well documented. It gives you a great flexibility and many hooks into Python internal. However, there is a price. There's no memory management in C and you should read and understand how the Python garbage collector works and what are the differences between a borrowed reference to an owned reference. Let's look at an example. Computing square root with Newton's method. Here is the C file. Line four and five we define a C…

Contents