python - 与早期版本相比,Python 2.7 的新 IO 库快了多少?

标签 python io python-2.7

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/

相关文章:

python - NiftyNet:索引超出范围错误

c - 优化 C 代码中的 I/O(输出)+ 循环

java - 如何在阻塞模式 NIO 中实现超时?

python - 正则表达式 - 选择字符串直到 '/'

python-2.7 - python 子进程 popen 返回 None

python - 将 python 脚本输出打印到文本文件和命令窗口

python - 是否有用于小型游戏的二维数组的 Python 模块/配方(不是 numpy)

python - bash 和 $DISPLAY,如何?

Python 加载二进制文件导致 1500% 内存开销

python - 如何迭代列表中的每个元素并将其与另一个元素相加以找到一个数字?