Question

The Python 2.7 update note says:

A new version of the io library, rewritten in C for performance.

I've played with Python 2.7 a bit, but I don't see any performance gain:

>>> from timeit import Timer
>>> t = Timer('f = open("E:\\db.txt", "r"); f.read(); f.close()')
>>> t.timeit(10000)

And the result:

  • Python 2.6.5 -- 12.879124022745913
  • Python 2.7 -- 12.905614540395504

Am I doing it wrong?

Was it helpful?

Solution

If you look at http://docs.python.org/library/io.html, the open() method in the io module isn't used by default for opening files in python 2.x. It was only in python 3.x which makes open() use io.open().

Try:

from timeit import Timer
t = Timer('f = io.open("E:\\db.txt", "r"); f.read(); f.close()', 'import io')
t.timeit(10000)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top