Book Image

Mastering Python High Performance

Book Image

Mastering Python High Performance

Overview of this book

Table of Contents (15 chapters)

ctypes


The ctypes library allows the developer to reach under the hood of Python and tap into the power of the C language. This is only possible with the official interpreter (CPython) because it is written in C. Other versions of it, such as PyPy or Jython, do not provide access to this library.

This interface to C can be used to do many things, since you literally have the ability to load pre-compiled code and use it from C. This means you have access to libraries such as kernel32.dll and msvcrt.dll for Windows systems, and libc.so.6 for Linux systems.

For our particular case, we'll focus on ways to optimize our code, showing how to load a custom C library and how to load a system library to take advantage of its optimized code. For full details on how to use this library, refer to the official documentation at https://docs.python.org/2/library/ctypes.html.

Loading your own custom C library

Sometimes, no matter how many optimization techniques we use on our code, they won't be enough to help...