News for hackers. Stuff that matters.
Re:Optimizing Python – A Case Study by clickok on Thursday May 28, 2015 @10:59PM (#9622453)
Serious question: if you have some code that really has to be fast, is it viable to keep it in Python, or should you ultimately end up rewriting it in a compiled language?

For example, I am writing code that implements networks that evolve over time for AI research. Prototyping it in Python makes it easy to test things out, but I expect that I will have to rewrite it in C++ or maybe something more fun, like Haskell[1].

1. Mostly for the sheer joy of trolling my colleagues with a learning agent monad.

Re:Optimizing Python – A Case Study by thezilch on Thursday May 28, 2015 @11:11PM (#9622487)
You might "just" migrate some slower parts to numpy or write a C target and interface with it over Python's CFFI.
Re:Optimizing Python – A Case Study by eikenberry on Friday May 29, 2015 @02:05PM (#9626267)
+1 numpy could probably handle this
Re:Optimizing Python – A Case Study by ma2rten on Friday May 29, 2015 @02:00AM (#9622941)
It depends on your use case.

In my experience the reason why you wrote something in python (implement features faster) remain valid reasons later on when you want to add functionality.

By using pypy, cpython, rewriting small parts in C/C++ and/or using the libraries which are written in C (such as numpy) you can normally make the hotspots in your code fast enough, while keeping the advantages of python.

Re:Optimizing Python – A Case Study by adrianN on Friday May 29, 2015 @03:19AM (#9623191)
In my experience it is worthwhile to first try and improve the performance in Python. It's easier to play around with different ideas in Python, and things like numpy quite often enable you to get "fast enough". Only if that is not enough should you consider writing a C extension.
Re:Optimizing Python – A Case Study by ryan_sb on Friday May 29, 2015 @02:25PM (#9626388)
Python makes it easy to do lots of things -- including very inefficient ones. Profiling your existing stuff and trying to optimize in pure Python often gets you pretty far.
Re:Optimizing Python – A Case Study by dec0dedab0de on Friday May 29, 2015 @10:38AM (#9624774)
I've never had to do this, but the common advice I hear is that the right algorithm in a high level language can be faster then the wrong algorithm in a low level language. So even if you do end up going the route of writing it in something lower level, it is likely worth optimizing your code at the higher level first where it is less expensive to try different things.
Re:Optimizing Python – A Case Study by p1esk on Friday May 29, 2015 @12:49AM (#9622734)
Same question to you: if you have some code that really has to be fast, is it viable to keep it in C++, or should you ultimately end up rewriting it for GPU?
Re:Optimizing Python – A Case Study by wallstop on Friday May 29, 2015 @01:00AM (#9622754)
Is this really a suitable counter-question? GPUs lend themselvse towards specific kinds of programming problems + added latency of GPU communication. On the contrary, the cost between switching from any on-CPU language is programmer time that may result in significant runtime advantages.
Re:Optimizing Python – A Case Study by vetinari on Friday May 29, 2015 @07:20AM (#9623887)
Also disadvantages: increased programmer time for implementing new features. Risks: you might optimize the wrong part (i.e. you invest into rewrite, but it will not solve performance problems).

That's why you must quantify advantages and disadvantages, including risks minimization and only then you'll see, whether given course of action is viable.

Re:Optimizing Python – A Case Study by jermy on Friday May 29, 2015 @08:13AM (#9624086)
You should probably first consider seeing if there are critical bits of code that can be rewritten to use MMX/SSE instructions, since your data is in the right cache already, without needing to move it anywhere else.