Python 2.7 update note说:
A new version of the io library, rewritten in C for performance.
我玩过 Python 2.7,但我没有看到任何性能提升:
>>> from timeit import Timer
>>> t = Timer('f = open("E:\\db.txt", "r"); f.read(); f.close()')
>>> t.timeit(10000)
结果:
- Python 2.6.5 -- 12.879124022745913
- Python 2.7 -- 12.905614540395504
我做错了吗?
最佳答案
如果你看http://docs.python.org/library/io.html , io
模块中的 open()
方法默认不用于在 python 2.x 中打开文件。仅在 python 3.x 中,open()
使用 io.open()
。
尝试:
from timeit import Timer
t = Timer('f = io.open("E:\\db.txt", "r"); f.read(); f.close()', 'import io')
t.timeit(10000)
关于python - 与早期版本相比,Python 2.7 的新 IO 库快了多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3412931/