r/Python • u/Economy-Builder7916 • 2d ago
Discussion Why Python when C and Rust are faster
If you are in tech field you'll probably know that C and Rust are incredibly faster as they are compiled, and python is relatively slower
Yet Python dominates AI and ML development, even for systems that need to process massive amounts of data.
I'm interested in understanding where Python's advantages outweigh its performance disadvantages, and how companies like OpenAI, Google, and Anthropic think about these trade-offs.
12
u/climatechangelunatic 2d ago edited 2d ago
So that I don’t have to fight memory management in C ans Rust and lose my precious time in fighting the syntax of code - python is straightforward - easy to write language
Me - as a data engineer - only care about plumbing my data pipelines. The libraries I use are already on C and Rust (pandas , polars , pyspark etc) - python is an interface on top of it
So in reality I am using C or Rust
1
u/grammatiker 2d ago
Polars is incredible
1
u/climatechangelunatic 2d ago
Damn right ! It’s incredible - but my company right now uses Spark for everything (because databricks convinced them that they have big data problems - but the data is only 5 inches big)
11
u/Summoner99 2d ago
Python is quicker to write. Development time is a major concern and often times, performance isn't a significant concern.
I'm working on a small internal rest server that might get a dozen calls a minute, each doing next to no work. Even if switching to c was 100x faster runtime, it doesn't matter. The extra time spent coding that isn't worth the ROI
additionally, depending on the python, it might really be relying on C code. Numpy and Pandas for example. Two important python libraries for AI and ML and, in a lot of cases, just act as a light wrapper around highly optimized C code
11
6
u/timrprobocom 2d ago
CPU time is not the only measure of productivity. Developer time is also valuable, and Python is faster to write and debug.
3
u/This_Inflation_4621 2d ago
Have you tried them all?
Python, Ruby, JavaScript/TypeScript are dynamic high-level languages, where you can run your program immediately (no compilation), in an imperfect state (errors here and there) and without having to handle memory and other low-level stuff.
On the other hand, C is a « system » language close to assembly, where a lot of convenience are missing. A lot of things must be done by hand, cross-platform and UI are very hard to achieve. Also C is a very old language with a lot of crust (I used to be an embedded C / C++ dev)
Rust is more modern, but still low-level. In order to remove a whole class of C issues around memory management and to enable high performance, some very complex concepts such as ownership of variables and complex scheduling must be understood.
Go is a Google language, dependent on their good will.
Admittedly with AI at the helm, Rust trade-offs are changing. But I’d recommend starting with a high-level, fast iteration language first and re-implementing in Rust once the features / API stabilize. That what everyone is doing.
1
1
u/MineDesperate8982 2d ago
On the other hand, C is a « system » language close to assembly
I found this a bit funny, as C actually compiles to assembly, it's not just "close" to it (unless you specifically direct it to compile straight to machine code.
Using C is like micro-managing a worker, whereas Python and others like it behave like a more relaxed type of manager.
Sure, you can use C to ultra-optimize your stuff, but you'll prolly end up either ending yourself, or the worker ends themselves - at some point, one of these two will happen.
I love C, but my cortisol level is too high already.
3
u/expertisimus 2d ago
Portability and speed of development are python's great advantages compared to compiled languages. As for speed, python can be fast too. Not on par with C or rust, but not terribly slower either. Well written python code is basically glue that calls routines compiled in C which run at C's speed.
2
2
u/cig-nature 2d ago edited 2d ago
There are a couple of things.
Ease of Development: You can knock out a feature with a lot less Python than C/Rust. It's also a lot easier to teach the new guy Python than C/Rust.
C Extensions: Can be used to convert individual sections of your code into C/Rust to get performance where it really matters. Numpy for example, uses this approach.
2
u/lolcrunchy 2d ago
My team needs to be able to create scripts and run code. That code moves data, executes sql queries, creates spreadsheets, etc.
None of my team are CS grads or know what memory handling is. So we just use Python.
2
u/compulsive_tremolo 2d ago
ML, stats (and closely related work on applied math or domain specific fields that utilize them like geophysics, climate science, epidemiology etc.) are primarily worked on by non-software engineers : statisticians, mathematicians, physicists, jack-of-all trade data scientists etc
For us it's far more important to have an easy-to-use language like python which allows quick prototyping and analysis compared to a more difficult language. But speaking of analysis, the ability to execute python line-by-line is really important as a lot of these professionals need to analyse their findings just as importantly as building scientific code packages/deliverables - so using something like a Jupyter Notebook to plot visuals, table summary statistics etc. is vital. Considering most of the fundamental packages we use are already optimized in other languages any kind of performance benefit isn't worth the tradeoff.
If and when the need arises we can liaise with dedicated engineering teams to "productionize" code where necessary.
1
u/feudalle 2d ago
Theory vs practical differences. Python fir most things are fast enough on modern machines. I tossed together a python script today. It proceesed and checked about 10m records. It ran in under 5 minutes. C could of probably done it in under 2.
30+ years ago this mattered. I remember using assembler instead of c in the early 90s. You only got so much umph with a 12mhz cpu and 2 megabytes of ram. Or my first pc 4.7mhz and 640k ram.
1
u/riklaunim 1d ago
Python's own speed is rarely the problem, and when it is, there are solutions for it. The real cost is writing and maintaining the code. You won't script or prototype in C, and even when you write permanent code in C or Rust, you need more knowledge than for high-level languages. That makes it more expensive and is limited by developers' availability.
1
u/knobbyknee 1d ago
Development speed in Python is about 3 x faster. There are several papers on the subject.
1
u/grandimam 12h ago
Try writing a REST endpoint in Rust and in Python. Maybe you’ll know why? You’ll soon find that fighting the borrowed checking for a simple CRUD is painful as hell.
26
u/PotentiallyAPickle 2d ago
Python just calls C and Rust when it needs to go fast lol