Faster Python with Different Implementations
Faster Python with Different Implementations. What’s not understood about Python’s performance is that there’s often a tradeoff between being a versatile, dynamic language, versus performance
Python’s getting faster. With Python 3’s release came a lot of performance enhancements.
What’s not understood about Python’s performance is that there’s often a tradeoff between being a versatile, dynamic language, versus performance. You can’t have everything. One of the best ways developers have dealt with the performance of Python is to use its alternative implementations. Yes — Python is a interpreted language — however, at runtime it’s compiled into bytecode that runs on a virtual machine. Different compilers can be used for different purposes to give you a performance edge.
This article will chronicle some of the choices out there.
This is the default compiler and the most widely used implementation of Python written in C. Cpython compiles python source code into intermediate byte code that is executed by the Cpython Virtual machine. There’s also Stackless Python which is CPython with an emphasis on concurrency.
This is one of the most popular alternative compilers used by Python developers who want performance. PyPy has a just-in-time compiler that compiles parts of your python code to increase performance. It also has some GC improvements that improve the memory usage of Python. There’s support for stackless mode that provides micro-threads for concurrency.
There are debates around whether PyPy is faster than CPython, but the general consensus is that it is faster, if not 4.4 times faster than CPython.
Jython or Jpython
Jython is the java implementation of Python. It allows the seamless integration of Python onto the Java platform. Java programmers will use it to embed scripting into large Java Applications. You can use Java threads to write multi-threaded programs. There are some performance gains but sometimes it’s even slower than CPython. Python can deliver added productivity in large scale Java application development.
IronPython is the implementation of Python that works closely with .NET. You can use the .NET libraries. Some developers like the fact that it has no GIL. This means that the performance for multi-threaded code is better. Some developers like the fact that you can write a web server using Python frameworks instead of ASP.NET. Here’s the IronPython vs. CPython Benchmarking thread.
Nuitka is not as mature as the other compilers but it compiles Python code into C/C++executables. It is fully compatible with Python versions from 2.6 to 3.7 and is twice as fast as CPython. Developers can use Nuitka to build a stand-alone executable of Python code even on Windows. You’ve got to find the proper use-cases for Nuitka, but here’s some guidance.
Do you use any of the above implementations of Python? Do you find them to be superior than CPython? What are the use-cases where you see the most benefits?
With different implementations of Python for different use cases, the default implementation will probably keep improving.