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

Steven D'Aprano steve at pearwood.info
Tue Mar 8 21:18:30 EST 2016


On Wed, 9 Mar 2016 12:28 pm, BartC wrote:

> (Which wasn't as painful as I'd expected. However the next project I
> have in mind is 20K lines rather than 0.7K. For that I'm looking at some
> mechanical translation I think. And probably some library to wrap around
> Python's i/o.)

You almost certainly don't need another wrapper around Python's I/O, making
it slower still. You need to understand what Python's I/O is doing.

If you open a file in binary mode, Python will give you a stream of bytes
(ordinal values 0 through 255 inclusive). Python won't modify or change
those bytes in any way. Whatever it reads from disk, it will give to you.

If you open a file in text mode, Python 3 will give you a stream of Unicode
code points (ordinal values 0 through 0x10FFFF). Earlier versions of Python
3 may behave somewhat strangely with so-called "astral characters": I
recommend that you avoid anything below version 3.3. Unless you are
including (e.g.) Chinese or ancient Phoenician in your text file, you
probably won't care.

In text mode by default, unless you tell it differently, Python will modify
the ending of each line so that it is presented to you as \n regardless of
whether the text file was created on a Mac, Windows, or Linux system. It
won't modify lines when you write them, only when you read them.

If you open a file in text mode, Python 2 will give you a stream of bytes,
but it too will modify the line endings.

In text mode, on Windows, Python *may* stop reading at a Ctrl-Z character
and treat it as End Of File. I think. I can across this once, many years
ago, but I no longer have access to Python on Windows to verify if that is
still the case.

There's more, but that's the basics to get you started.



-- 
Steven




More information about the Python-list mailing list