• How Slow is Python?
  • Originally by Doug Foo
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: Alfxjx
  • Proofread by: Wu-Yikun

We all know it’s slower than C, Java, and Rust, but just how slow is it?

Just as I was starting to think about the article, a joke post “Python isn’t actually slow” popped up in my feed – and I read it even though I knew it wasn’t true and was just clickbait. When it comes to Python, it’s like you’re dealing with snails, and while you can train a snail well, you can’t get rid of its basic genetics (unlike Ethan Hawke in the classic movie Gattaca).

The benchmark

So I started writing some simple benchmarks to cross-test C, Rust, Java, and Python. Here are my main criteria:

  • Managing memory stress (object creation/destruction)
  • Json-like string handling
  • Circular and computationally intensive operations

For reference, there is a great GitHub project that evaluates the performance of a dozen languages, and the results are shown in the table below. Using C++ performance as baseline 1, Python performed 227 times slower on the brainf test (an interesting Turing machine interpreter) than C++.

I ended up writing very simple snippets of code to test because I realized I had forgotten how most of these languages were programmed. I had to reset my testing goals to fit my weak skills…

Analysis of test results

I’ll link to my GitHub for a real hacky test in the Reference section. I want the naming to be self-explanatory.

Here’s my summary of the different language features:

  1. Python file I/O is fairly fast because the limiting factor is disk
  2. Python is unusually slow at recursionAlone,fib(30)The Fibonacci sequence of order recursion is already ridiculously slow
  3. Python function calls are slow – one of the causes of slow recursion
  4. Java without JIT optimization can be really slow, and in some ways even slower than Python
  5. Java native String + is still very slow (100 times), and while Using StringBuilder makes it a lot faster, it’s still slower than Python
  6. Java with a JIT is so fast today that C is hardly worth using anymore…
  7. Rust is really fast, though it could be faster if I knew how to write Rust code better……

While benchmarks that are 200 times slower have yet to be proven in my mini-test, it’s clear that with a few extra function calls and recursion, you could easily reach this conclusion.

So why is Python slow?

Most of the reasons are obvious, but let me list them too:

  1. Python is an interpreted language, and although compiled bytecode, it is not really optimized
  2. Python uses garbage collection, but mainly reference counting, so it is faster, or at least more deterministic, than Java
  3. No JIT compiler by default – seems essential for interpreted languages (PyPy uses it 10 times better)
  4. Being a weakly typed dynamic language has its slowness (and makes it more difficult to build jIts)
  5. Function call implementation is unusually slow (maybe some stack frame allocation algorithms are complicated?)

Note that we didn’t even test multithreading or multiprogramming, as we all know Python also has GIL (global interpreter lock) issues.

How can I optimize Python for speed?

There are a few tricks to speed up Python, but most of them aren’t very good.

  1. Use multiple processes to handle worker threads (but be aware that you are limited by GIL)
  2. Write native C code and link to Python
  3. Using native Python functions (written in C at run time)
  4. Single-line list derivations seem to be performance-optimized, so they seem like more than just cool tricks

To be honest, I don’t see a lot of great tricks… Some tricks are like “Use O(n) and O(n-log n) algorithms instead of O(n²)”… Maybe the first tip should be to learn some computer science?


Refer to the article

  1. Making the Python fast – github.com/ajcr/ajcr.g…
  2. Python Slowness – medium.com/analytics-v…
  3. Why is Python Slow — hackernoon.com/why-is-pyth…
  4. GitHub of hack test1.* sample s — github.com/dougfoo/med…
  5. Lack of the ByteCode optimization in Python – nullprogram.com/blog/2019/0…

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.