views:

228

answers:

1

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?

+3  A: 
David Morrissey
Oops, I was looking at the Python 3.2 docs. Looks like I've also misread the sentence. It's the Python 3 IO library (with Unicode as the default codec) has been back-ported to Python 2.7. The default IO in Python 2.x is already written in C and has decent performance.
Wang Dingwei
>>>t = Timer('f = io.open("E:\\db.txt", "r"); f.read(); f.close()', 'import io') --> I get 30.724059342737746, similar to Python 3.x
Wang Dingwei