Pyhon 2.x or 3.x, which is faster?

Chris Angelico rosuav at gmail.com
Mon Mar 7 20:49:47 EST 2016


On Tue, Mar 8, 2016 at 12:34 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Tue, 8 Mar 2016 07:47 am, Chris Angelico wrote:
>
>> You write *real world* code and then profile that. You get actual real
>> programs that you actually really use, and you run those through
>> timing harnesses.
>
>
> Chris, I think that's exactly what BartC has done: he has a program that he
> actually uses, one which processes JPG files. It's written in pure Python,
> so he's not comparing the quality of C libraries, he's comparing Python
> versus Python.

The trouble is that we can't be sure _what_ he's done. All we have is
that it takes longer under some circumstances than others. Without
knowing a lot more about how the measurements are done, I don't know
that we can get anything from it.

And I remember seeing a performance analysis that showed that
array.array() actually sucks for performance; hence my recommendation
to try other ways of doing things, before saying "Python 3 is slower
than Python 2".

> Could Bart's code be improved for production use? Almost certainly. I'm sure
> that by using a C image processing library, like pillow, it would be ten or
> a hundred times faster. If Bart were saying "Python is crap, it's too slow"
> then a perfectly acceptable response would be "no, you're just misusing it,
> here you want to use it as an interface to this library and let the library
> do the heavy lifting". That's what Python is designed for. But that's not
> what Bart is saying.
>
> I'm impressed that pure Python code running in CPython is even *usable* for
> whatever sort of image processing BartC is doing. He must be doing
> something right, given that its not unusably slow.

Fair point. Although these are only small files (as far as I know), so
even if there's an O(N**2) memory allocation (eg with a naive "take a
byte off the front and give me back the rest" algorithm), it'd quite
probably still be usable. I've had N Squareds in code that sat there
until the day I did some stupid benchmark on a gig of data, and only
then started seeing problems. Readably-written code isn't necessarily
that much slower than tightly-optimized code.

ChrisA



More information about the Python-list mailing list