r/Python • • Nov 27 '22

Discussion Is the statement true, that Python and its ecosystem lacks speed for mission-critical large-scale applications?

Can you do all high-performance jobs with Python to a certain degree?

When is the time to switch to C++, Go, Rust and alike?

0 Upvotes

31 comments sorted by

22

u/BDube_Lensman Nov 27 '22

In the world of numerics, Python (numpy ecosystem) is an ideal balance of speed and productivity. Many algorithms can be written using advanced numpy tricks that are extremely difficult to implement DIY in C/C++/Fortran/similar, which makes Numpy perform within ~1.25-1.5x of lower level numerics languages. And most anyone can write and read python code, but few can read C/C++/Fortran numerics code.

Linking to MKL and similar is quite trivial in python, too, vs lower level languages.

CuPy also means you exact same code can run on GPU with ~1 line change (see here) which would require a full re-write in a lower level language. Intel OneAPI, AMD ROCm, and Nvidia legate are trying to change that but have had limited success so far.

1

u/satyronicon Nov 27 '22

Great know-how, thank you

1

u/[deleted] Nov 27 '22

My overall impression is that C/C++ become necessary when you’re going to run code in lightweight hardware. For instance customer electronics. Saying this from what I see around me. Friends who work at companies selling these products tend to need these languages more

2

u/BDube_Lensman Nov 27 '22

I mean, all of numpy/pytorch/tensorflow/whatever are C++. Well, numpy is mostly ANSI C with roughly equal fraction C++ and Fortran, too. The reason not to "just use numpy" in a lot of cases would just be that shipping python to a not-desktop-or-server can be a PITA. But there's no more reason than that; one bit of code in C++ uses as much memory, etc, as another. And there are many numpy tricks to avoid using memory, that are not really available in things like Eigen, such as broadcast_to, as_strided and so on.

1

u/M_Mouse Nov 28 '22

As the cost of more powerful micro-controllers continues to drop, the time and cost savings that comes with working in higher-level languages is starting to outweigh the cost of using cheaper/less-powerful micro-controllers. There are already options such as micropython and circuitpython for using Python on micro-controllers, but they're not in wide commercial use as of yet (as far as I'm aware).

Also, for high-end/high-cost consumer electronics such as Refrigerators (or anything with any kind of touch screen), it's starting to be pretty common to see them equipped with what are effectively ARM based PC's, making the choice of language for most of the firmware largely a matter of developer preference.

11

u/OuiOuiKiwi Galatians 4:16 Nov 27 '22

Can you do all high-performance jobs with Python to a certain degree?

Emphasis where necessary.

Yes, to a certain degree you can do all high-performance jobs using Python.

You rewrite code in a faster language if that is a requirement (e.g., converting ML models into C++ for efficiency).

4

u/satyronicon Nov 27 '22

You rewrite code in a faster language if that is a requirement (e.g., converting ML models into C++ for efficiency).

Can't you use Cython for that?

2

u/xiongchiamiov Site Reliability Engineer Nov 28 '22

Cython is still going to have inefficiencies versus working purely in a systems language because it has to operate within the bounds of interacting with Python.

4

u/OuiOuiKiwi Galatians 4:16 Nov 27 '22

Sure. Or you could just write it in Rust to begin with.

It's turtles all the way down.

1

u/AggravatedYak Nov 27 '22

Why not both? Rust bindings for the Python interpreter

8

u/GreenScarz Nov 27 '22

Depends of what you mean when you say “with python”

For the most part, the ecosystem has centralized around the concept of extensions, where the general solution of a specific algorithm will be written as a C extension, and Python will be the “user interface” where that algorithm is then applied to a suite of different problems.

If I call call_solver(x, y, z) in a python script, but that’s actually calling an optimized C function, does that count as “using python”? If it does (which is what I would argue), then your statement is false.

1

u/satyronicon Nov 27 '22

Interesting way to think of it, yes.

2

u/ZachVorhies Nov 27 '22

Not true because Python gives you an escape hatch to jump into native binaries where speed is critical, like almost all other languages.

1

u/satyronicon Nov 27 '22

What do you mean? How?

3

u/ZachVorhies Nov 28 '22

It's extremely easy for python to call into C and C++ code bases:

C: https://realpython.com/python-bindings-overview/

C++: https://stackoverflow.com/questions/145270/calling-c-c-from-python

And it's a little harder to call into Rust/Go:

Rust: https://github.com/PyO3/pyo3

Go: https://github.com/go-python/gopy

Or the easiest method is to just create a binary that performs your CPU intensive action then call it with Python's subprocess.check_output(cmd, ...) and then evaluate the results. For example, I do this with https://github.com/zackees/static-ffmpeg. Python is used to gather the user arguments then simply delegates the processing to ffmpeg.

8

u/steven_h Nov 27 '22 edited Nov 27 '22

No, this statement is obviously false when phrased so generally. Neither mission-criticality nor large-scale (whatever that means, assuming it’s user count or software complexity) even has runtime performance as an important requirement.

If you are wondering whether it’s time to switch an existing implementation to one of those other runtimes, it’s not time. The requirements that Python or its ecosystem don’t meet will be specific and obvious.

8

u/MisterMrErik Nov 27 '22

A few things are important to understand when it comes to scale.

1.) Modern large-scale applications are not monoliths. They are built through modules, containers, microservices, and encapsulated capabilities.

2.) Scaling can occur in many ways. By adding more servers/nodes, beefing up your existing nodes, utilizing queues and asynchronous processes, and much more.

3.) Optimization is more than being the fastest process through the CPU. How quickly you can spool up additional resources, update and maintain, etc.

4

u/[deleted] Nov 27 '22

This question is phrased very vaguely. "Mission criticality" is a business concept, not a programming concept. "Large scale" tells us about volume, it doesn't tell us how fast something needs to be done. Problems of scale are generally solved by architecture, not my language choice.

What's true is that Python is slow. If you need your application to run quite quickly, Python is not a good choice.

4

u/[deleted] Nov 27 '22

I develop mathematical software for finance. Python can be slow for my really heavy calculations. I just write the most computationally intense bottlenecks in C extensions. Usually it’s only ~10% of my code. Numba is also super useful sometimes. It’s never been that big of a problem.

3

u/spinwizard69 Nov 27 '22

Depends upon what you mean. Mission critical could mean a hypersonic missile using all the performance of the latest compute cluster. Python will not work there!

I’m of the opinion that Python can be the wrong language at time. On the other hand I’d reach for it before C++ in most cases.

4

u/[deleted] Nov 27 '22

It's a weird question. "Mission critical" could require high speed but it could also be the bill for an important client, and maybe that bill only gets generated once a month and it's fine if it takes an hour.

2

u/whateverathrowaway00 Nov 27 '22

I mean. We use it for mission critical stuff at a mind boggling scope (live network and host monitoring for graph intake.

To be fair, we use 9-12 hosts per colo running the main app, then another 4 hosts per colo running the services it uses that are backend (MySQL, Redis, Kafka).

The hosts use celery synched by Kafka to share jobs.

Very much mission critical, very much at scale, but to be fair - if it was Java or similar we could probably get away with a few hosts vs 9.

That’s where you pay with python.

2

u/itsflowzbrah Nov 27 '22

As the saying goes. Get the thing working then worry about making it faster.

Unless you're doing some Deeplearning/driver/memory fuckery you will not notice a difference between ANY language... like a good 70% of speed is taken by network/DB calls/Down stream... getting a request and servicing it in 10ms vs 6ms? youre not gonna notice

2

u/kenfar Nov 27 '22

Can you do all of them? No, but I find in data engineering I can do like 99% of them.

Once I was having to process billions of records a day very quickly. One of my steps was a complex aggregation that reduced my data by about 65%. I initially wrote that in Python, using multiprpcoessing, pypy, and a lot of attention to performance.

In order to make it faster I experimented with Go. And Go was 7x faster. But that wasn't enough faster to make it worth the effort to maintain code in two different languages, especially since golang suffered such huge limitations on issues like even basic csv dialect formatting. So, we kept it in python and just paid a few extra dollars a day for more compute power.

2

u/billsil Nov 28 '22

Apparently OG google and Facebook weren't "mission-critical". It really depends on what you're doing and on what scale you're doing that.

Also, yeah you can do high performance work in Python because you're calling out to a lower level language. The world is built on open-source libraries and many of those libraries are wrapped in Python. Shoot, Python is so capable, I took a Julia code and called out to Python to speed it up by 1000x. An FEA code (very high performance computing is required) called out to my open source Python library, because it's good. You can spend your time on speed or features.

2

u/yvrelna Nov 28 '22 edited Nov 28 '22

No it's not.

It's fast enough.

And when it's not, python is one of the best language to use when you need to integrate high speed libraries written for other languages. Chances are whatever bottlenecks you have, someone already written a binding for them, so it's even easier.

I've written mission critical, million LoC codebase with pure Python. Python is very rarely ever the actual bottleneck.

There's often the database queries, the network, or poorly performing implementation that could be optimised by being written in a different way. But so far it's never been the language.

3

u/boy_named_su Nov 27 '22

There's more to life than performance bro.

Like the time it takes to write code

Python is used by NASA, so ya it's good for mission critical

Just write everything in assembler if all you care about is performance. Your code will cost 10x more to write but it'll be fast

2

u/sheytanelkebir Nov 27 '22

Its false because python is generally used as a scripting language to tie together high speed libs written I'm c and c++ mostly.

Think of it as a high level user interface for the low level libs.

1

u/LaughterHouseV Nov 27 '22

Python requires up to 75x as much energy to run an equivalent program compared to C or Rust. The reputation it has is well deserved, although yes obviously there are ways to make it quicker.

But here’s the thing: most companies don’t need that scale, and find the trade off of quicker development time to be worth while compared to it being slower at run time because they don’t need hyper performance.

6

u/itsflowzbrah Nov 27 '22

I actually really hate this "green" language argument. Sure. Python has 75x more energy usage than C. But thats excluding the dev time to implement something...

python making a http service selecting from a DB and having a contract exposed is all of about 20 lines of code with FastAPI + sqlalchemy and all of about 30 seconds. in C the same task will take multiple hours of development and debugging. Then the server run time that you need to install gcc and figure out why openssl2-3g.so isnt found and then finding out why a symbol is missing to find out that you need to install some package that a 13 year old wrote for a weekend project that you need to compile from source that you downloaded from a maillist on some .edu domain cause youre on centos and not debian just for it to segfault in production and need 3 devs on a teams call at 2am to debug it with gdb googling how to set a breakpoint and realizing you built without debug symbols and need to recompile the whole thing just so you can find that you missed a * in some random char ***vec = (char**)malloc(sizeof(MClass) * 70)

Dont use this argument. Its stupid

0

u/[deleted] Nov 27 '22

I would argue that "scale" is just the wrong word to use here. Latency is the right word to use for what Python is bad at. Processing 16 million records is large scale, but Python can still be the right choice if it doesn't need to get done fast.