DEV Community

loading...
Cover image for Have you had to make your Python code run fast?

Have you had to make your Python code run fast?

Dustin King
Python. Webdev. Music. Also, other stuff.
・1 min read

Though performance is often cited as a reason people are skeptical of Python (see @grokcode 's recent post and the replies to it ), I haven't run into many performance issues in Python myself. On the other hand, most of the programming I've done in Python hasn't been the kind where milliseconds matter (or if they did matter, it was already optimized by libraries like NumPy and Matplotlib).

The one performance issue I had to deal with wasn't with Python itself, but with having to await the results of many database queries, and therefore needing to have multiple ones running at a time. I spent some time trying to get multiprocessing to work, before realizing that threading was sufficient (as the Python code itself wasn't CPU-bound).

Have you run up against performance issues in Python, or had to use it in a resource-constrained environment? What kind of problems did you have? What did you do to meet your performance needs?

Edit to add: If you've never run up against performance issues in Python after using it a lot, especially if there was some situation where you you thought performance was going to be a problem, I'd like to hear about too.

Discussion (6)

Collapse
ben profile image
Ben Halpern

The one performance issue I had to deal with wasn't with Python itself, but with having to await the results of many database queries, and therefore needing to have multiple ones running at a time.

I think it's often a matter of picking the right tool for the job. In web development, it rarely seems like the language is the most important bottleneck.

Of course, it's a factor, but other things take precedence.

In other areas of software, the language's performance winds up playing a bigger role. Don't write an OS in Python!!

Collapse
xowap profile image
Rémy 🤖

Did some financial calculation in real-time. It was taking minutes but with appropriate pre-calculations and a smarter algorithm it went down to 100ms. Yay. Well that's why algorithm complexity matters and also it applies to the whole system and not just your piece of code.

Collapse
tux0r profile image
tux0r

Yes, I did. It took ages to render a page. I fixed it by dropping Python completely. (Not joking.)

Collapse
cathodion profile image
Dustin King Author

Can you elaborate on that? What framework/etc. were you using? What did you end up using?

Collapse
tux0r profile image
tux0r

I used Flask. I ended up using C behind FastCGI.

Thread Thread
cathodion profile image
Dustin King Author

I'm interested in hearing a little more about your journey from A to B.