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

Michael Torrie torriem at gmail.com
Tue Mar 8 19:42:37 EST 2016


On 03/07/2016 11:34 AM, BartC wrote:
> (I'm quite pleased with my version: smaller, faster, works on all the 
> Pythons, supports all 3 colour formats and no decoding bugs that I'm 
> aware of, and it's the first Python program I've written that does 
> something useful.)

I think you should be commended for writing it.  It may not be perfect,
nor is it necessarily "Pythonic" but you had fun doing it.  I hope you
aren't discouraged by the criticism of your code.

I hacked on it a bit, and it only took about a minute to change from
using arrays to immutable byte strings.  The only problem with that was
what I noted in my other reply.  That is that indexing a Python 2 string
gives a different result than indexing a Python 3 byte string.  I'm not
sure what kind of logic should be used to efficiently judge between
Python 2 and 3, and modify the answer accordingly.  In Python 2 I can
simply do "ord(fs.data[fs.pos])".  In Python 3 it's just
"fs.data[fs.pos]".  I'm not sure what kind of speed up using a normal
string provides vs the array.  But it has to be more a bit faster.

I suppose if others want to direct you in a more Pythonic path, they
could take your code and turn it into something that is more idiomatic
(and perhaps faster).  I may take a crack at it.

If it were me I think I'd make your jpeg decoder into it's own class,
and have it operate on any iterable input, be it an open file or
something else.  That may be actually slower though, but more pythonic.





More information about the Python-list mailing list