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

Meme pythonSlow

Post image
12k Upvotes

420 comments sorted by

View all comments

Show parent comment

29

u/yonasismad 6d ago

Python compiles into byte code.

31

u/FalafelSnorlax 6d ago

Yeah but isn't a lower-level language, it's a machine-friendly format of the language. The main point is that by virtue of being interpreted, it is inherently different to languages like C/C++ or Rust.

13

u/toejam316 6d ago

Inherently whatever the code is at some point "compiles" into the language the CPU uses. The fact it's running in a JIT interpreter doesn't prevent that. If you were insane enough you could absolutely get the assembly equivalent of your python code.

3

u/m0nk37 6d ago

Python loads the C/C++ library that runs the machine learning, python listens for events from the library and delivers it to you. Thats what a wrapper is, there is no byte code actually doing the machine learning through python, python is just the intermediator in that situation.

2

u/toejam316 6d ago

I wasn't talking about Python interacting with other libraries, I was talking about the execution of the code itself. It's literally impossible to run code on a system that doesn't at some point convert into the appropriate machine code for the platform it's executing on.

1

u/m0nk37 5d ago

K but less interaction for a better result matters. 

1

u/toejam316 5d ago

We're not discussing the best solution here.

7

u/nimrag_is_coming 6d ago

Python doesn't JIT compile, and it can't really very easily due to how dynamic it is, ie every time a piece of code runs it might actually run something different depending on what type a variable is etc.

You probably technically could turn it into assembly but then it's basically going to be a virtual machine with abstract types rather than being like actual raw assembly, so you've gained almost nothing compared to just running bytecode in an interpreter.

15

u/i_only_eat_purple 6d ago

CPython (the reference implementation that most people use) has an experimental JIT compiler since 3.13. https://peps.python.org/pep-0744/

7

u/rosuav 6d ago

PyPy JIT-compiles, and CPython currently has a JIT compiler too, although that might be removed.

14

u/Ok-Scheme-913 6d ago

Tell that to the multiple implementations of Pythons that are jit compiled, like GraalPython.

A programming language is just that, a language. Sure, some are easier to compile to efficient machine code than others, but there is no law forbidding a sufficiently smart compiler to work 3 days and prove that this ultra efficient simd machine code is the same program denoted by python's semantic laws, and thus that will be the compiler output.

4

u/nimrag_is_coming 6d 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 6d ago

So?

5

u/Terrible_Activity819 6d 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.

0

u/danielv123 6d ago

Like sure but it's slow. C/c++/javascript/go/java is fast.

Like, I understand that it doesn't matter that it's slow as long as you just call some other software, but it would still be nice if you could write a loop without a 100x performance impact.

6

u/thefireskull 6d ago

javascrip is fast.

loop without a 100x performance impact.

Is this JS hyper fast loop in the room with us?

1

u/danielv123 6d ago

In for loops, js is generally 10 - 50x faster than python. JS isn't exactly known for its speed.

1

u/DeGloriousHeosphoros 6d ago

There are much faster methods of doing things than loops in Python, and particularly in JIT-compiled optimized implementations like PyPy, or using numpy, scipy, numba, and other C-extension libraries that skip the normal interpreter entirely. Additionally, you can do certain things very quickly with certain techniques like using aggregation functions (e.g., builtins map, sum, filter, max, min, etc.), caching (e.g., @functools.lru_cache), multiprocessing, using the right tools for the job (e.g., use a parser for parsing a JSON file rather than doing things line-by-line, str.join instead of iterative string appending, etc.), and you can always use a FFI or some other tool/code for highly time-critical segments of code.

1

u/danielv123 6d ago

Sure, but why would I learn the DSL of one of the many tools with python bindings when I could do a 1:1 syntax translation to JS and have it be a few dozen times faster.

The whole argument of "python is fine, because the performance doesn't suck as long as you don't use it" seems silly to me.

1

u/FabioTheFox 6d ago

JS is at least JIT compiled through V8 it's not unlikely loops can be done pretty quickly

That is if you use Node and not the heavily nerfed V8 that runs in browsers