Python has never been as speedy as C or Java, but several projects are in the works to get the lead out of the language. Credit: Jamesboy Nuchaikong / Shutterstock Spiffy and convenient as Python is, most everyone who uses the language knows it’s comparatively creaky—orders of magnitude slower than C, Java, or JavaScript for CPU-intensive work. But several projects refuse to ditch all that’s good about Python and instead have decided to boost its performance from the inside out. If you want to make Python run faster on the same hardware, you have two basic options, each with a drawback: You can create a replacement for the default runtime used by the language (the CPython implementation)—a major undertaking, but the result would be a drop-in replacement for CPython. You can rewrite existing Python code to take advantage of certain speed optimizations, which means more work for the programmer but doesn’t require changes in the runtime. Here are six ways the bar on Python performance is being raised. Each uses one of these two approaches, or a combination of the two. PyPy Among the candidates for a drop-in replacement for CPython, PyPy is easily the most visible (Quora, for instance, uses it in production). It also stands the best chance of becoming the default, as it’s highly compatible with existing Python code. PyPy uses just-in-time (JIT) compilation, the same technique used by Google Chrome’s V8 JavaScript engine to speed up that language. Although PyPy used to favor Python 2 over Python 3, the most recent versions of PyPy support Python 3.6 and Python 3.7 as well as Python 2.7. Another long-standing drawback was that PyPy didn’t integrate well with common libraries used to accelerate Python performance, such as NumPy. However, recent releases go a long way towards addressing this problem. PyPy still has other limitations. It’s best for long-running programs like servers, rather than one-and-done scripts, as its performance benefits don’t really register until after some warmup time. And its executable has a much larger footprint than CPython. Pyston The Pyston project, originally created by Dropbox but since relaunched and rewritten, also uses a JIT to speed up Python. Its original incarnation used the LLVM compiler infrastructure to do this, but the rewrite dropped LLVM in favor of a hand-rolled assembler with much lower overhead. The rewrite also uses CPython code as the basis for the project, so it’s far more compatible out-of-the-box with conventional Python. Pyston’s speedups are not very dramatic yet—about 20% faster, on average—but the project is still very much in its infancy. Nuitka Rather than replace the Python runtime, some teams are doing away with a Python runtime entirely and seeking ways to transpile Python code to languages that run natively at high speed. Case in point: Nuitka, which converts Python to C++ code—and can automatically pack up all of the files needed from the CPython runtime to boot. Long-term plans for Nuitka include allowing Nuitka-compiled Python to interface directly with C code, allowing for even greater speed. Cython Cython (C extensions for Python) is a superset of Python, a version of the language that compiles to C and interfaces with C/C++ code. It’s one way to write C extensions for Python, which wrap C or C++ code and give it an easy Python interface. But Cython can also be used to incrementally accelerate Python functions, chiefly ones that perform math. The downside is that Cython uses its own peculiar syntax to work its magic, so porting existing code isn’t totally automatic. That said, Cython provides several advantages for the sake of speed not available in vanilla Python, among them variable typing à la C itself. A number of scientific packages for Python, such as Scikit-learn, draw on Cython features like this to keep operations lean and fast. Numba Numba combines two of the previous approaches. Like Cython, it speeds up the parts of the language that most need it (typically CPU-bound math); like PyPy and Pyston, it uses JIT compilation. Functions compiled with Numba can be specified with a decorator, and Numba works hand-in-hand with NumPy to accelerate the functions found. In fact, Numba works best with libraries it is already familiar with, like NumPy. typed_python The typed_python project, a nascent effort supported by A Priori Investments, takes a different approach from any of the above. It provides a collection of strongly typed data structures for Python that are restricted in the types they can hold. For instance, one could create a list that only accepts integers. With this, one can then generate highly optimized code that runs faster and takes advantage of processor parallelism where possible. One can write the majority of the program in conventional Python, then use typed_python within a specific function to speed up its operations. This is akin to how Cython can be used to selectively speed up the parts of an application that can be a bottleneck. Python creator Guido van Rossum is adamant that many of Python’s performance issues can be traced to improper use of the language. CPU-heavy processing, for instance, can be hastened through a few methods touched on here — using NumPy (for math), using the multiprocessing extensions, or making calls to external C code and thus avoiding the Global Interpreter Lock (GIL), the root of Python’s slowness. But since there’s no viable replacement yet for the GIL in Python, it falls to others to come up with short-term solutions—and maybe long-term ones, too. Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos