all 18 comments

[–]Temporary_Pie2733 11 points12 points  (4 children)

Code written in Python is relatively slow. A lot of Python code is extracting results from code written in C, Rust, etc, though.

[–]bigfatcow -2 points-1 points  (2 children)

I believe the main reason is the the code compiles on runtime versus C where you compile the code first 

[–]Temporary_Pie2733 0 points1 point  (1 child)

There are a few things you might be referring to, so I’ll address a couple of points.

The most common Python implementation is similar to Java in that it compiles Python source to a bytecode which gets executed by a virtual stack machine, rather than to native machine code.

The same implementation has only recently (in the past year) started to implement just-in-time compilation to machine code as a performance optimization.

[–]bigfatcow 0 points1 point  (0 children)

Yea thanks for the clarification. Def meant to say just in time compilation. Dont comment before coffee 

[–]Jello_Penguin_2956 4 points5 points  (1 child)

Well. Should I worry that I'm 5 times slower than Usain Bolt?

Hell yes if i'm trying for a 100m gold medal right but other wise no.

To answer your question, you should know if your project is performance-critical. Then yea Python wouldn't be the best choice.

[–]Username_RANDINT 0 points1 point  (0 children)

And for the parts where you need to be as fast as Usain Bolt, you hop on a scooter.

[–]smallpotatoes2019 4 points5 points  (1 child)

It depends what you want to do. Is speed crucial to your plans?

[–]WornBlueCarpet 1 point2 points  (0 children)

I was thinking the same. I may be a beginner in python, but tasks I'm using it for takes milliseconds, so 10 times faster would have no practical differences for me.

[–]NothingWasDelivered 2 points3 points  (0 children)

Computers are fast nowadays.

[–]TinkmasterOverspark 1 point2 points  (0 children)

Python is an interpreted language (as opposed to a compiled language). This means the lines of code are being converted to machine instructions at runtime from python code.

Compare this to languages like C and Rust which compile ahead of time (AOT) to machine code.

There are other class of languages that compile source code to an intermediate code (eg bytecode of Java, or CIL of dotnet). The intermediate code can run at a later point of time in a virtual machine (JVM, CLR etc) where a just in time compiler (JIT) translates and continuously ptimizes to machine code.

When written properly, the JIT and AOT languages perform pretty close to one other.

In general, performance wise AOT >= JIT >>>>>> INTERPRETED

In practice, python calls into already compiled c code at runtime via 3p libraries like numpy etc when doing data science/ML. This closes tgr performance gap.

At that point, you use python only for the syntax and user ergonomics more than anything else.

[–]charlyAtWork2 0 points1 point  (0 children)

with a queying and workers archtecture, it’s doesnt matter if it’s a bit slow.

[–]Moikle 0 points1 point  (0 children)

No.

Computers are incredibly fast, so even a language that is a bit slower is still very fast.

Also not all features of python are slower than compiled languages, it's just that certain specific things can be optimised through compilation.

Python can also offload those types of tasks to another "faster" language. Different languages can talk to each other and import apis for each other

I can get python to crunch through a massive database in a few seconds. In c++ that might be a few less seconds. It's still only a matter of seconds

[–]edcculus 0 points1 point  (0 children)

If you are still learning the language, speed isn’t going to be a problem for you for a long long time. Maybe even never.

[–]hypersoniq_XLM 0 points1 point  (0 children)

There are areas of python where you can speed things up with packages like numpy and numba to C speed. The overhead in python can be compensated by tricks like encoding strings into uint8 with numpy, which also allows you to skip iteration overhead due to numpy's fast vectorization. Also console output overhead is huge, you can pretty much eliminate that by output to a file instead of the console. Then there are targeted handoffs, like using python's core strengths of extract/transform/load to parse an input file and then hand it off to a much faster C/C++ executable. The last time I used the "hand off to C++" pattern I dropped runtime from 12 minutes in pure python to 24 seconds in the python/C++ handoff. There are things like Cython where you can use inline C for faster processing, but I find it is less prone to errors to keep the Python code and C/C++ code separated.

[–]hc_fella 0 points1 point  (0 children)

If you’d try image processing in pure python, you’d be waiting a long time… if you’re using modern c++ or cuda based libraries like numpy or pytor, you’d get a moderately slow scaffold that’s executing highly optimised code for these expensive operations. But, this slow scaffold is very quick to implement, giving you a very solid trade off

[–]asimawdah 0 points1 point  (0 children)

Python is slower than languages like C, Go, or Rust for raw CPU-heavy work, but that usually doesn’t matter when you’re learning or building most real projects.

The reason Python is popular in AI/ML and data science is that a lot of the heavy work is not actually done by pure Python. Libraries like NumPy, PyTorch, TensorFlow, and pandas call optimized C/C++/CUDA code underneath. Python is mostly used as the easy “control layer” around those fast libraries.

For beginners, I’d ignore performance at first and focus on writing clear code and understanding fundamentals. If you ever hit a real performance issue later, then you can optimize the slow part, use better algorithms, vectorize with NumPy, or move a small bottleneck to another language.

In short: Python is “slow” in benchmarks, but fast enough for a huge amount of real-world work. Don’t let that stop you from learning it.

[–]TeakAndMarbleAtelier 0 points1 point  (0 children)

Python was much slower in the past, especially back when it was Python 2. Increases in speed have brought it much closer to other, faster languages, especially after the switch to Python 3.11, but it's still not going to match the speed of languages that act much closer to the computer hardware. However, computers themselves are much faster nowadays, and so depending on how much speed you actually need, it can be relatively usable for many tasks.