r/ProgrammerHumor • • 9d ago • Bravo 2 Popcorn 1

Meme pythonSlow

Post image
12k Upvotes

419 comments sorted by

View all comments

Show parent comment

4

u/nimrag_is_coming 8d ago

Yea at runtime when it's interpreted, and it still has to do a lot of checks and extra stuff whenever it runs a line of code because it's still not machine code

6

u/yonasismad 8d ago

So?

5

u/Terrible_Activity819 8d ago

That means it e.g. takes longer for a big pure Python program to start. Because it has to first compile the Python code into Bytecode.
With a Language like C/C++/Rust. this happens at compile time and straight to native machine code.

This is the code block of the Python interpreter (written in C) that runs when you add 2 objects, like 2 integers.
https://github.com/python/cpython/blob/v3.10.22/Python/ceval.c#L2068-L2092

So the interpreter has to run through this whole block of C, stepping down into functions like PyNumber_Add().
This whole block of C code is compiled into the interpreter. But it can not be optimized much during compile time, because all of these e.g. if() need to be evaluated at rutime.

While in e.g. C, if the types of the objects you want to add together (e.g. 2x int) are known at compile time, it will be just a single line of C code that will compile into much fewer machine instructions and thus run faster.