开发者

How much faster is Python 2.7's new IO library compared to earlier versions?

开发者 https://www.devze.com 2023-01-10 13:17 出处:网络
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:

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?


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消