Eri*_*itt 2 python performance bufferedreader python-3.x
我有一个在Python 2和Python 3中运行的程序,但速度有很大差异.我理解在交换机中进行了一些内部更改,但io.BufferedReader的差异非常大.在这两个版本中,我都使用io.BufferedReader,因为主程序循环一次只需要一个字节的数据.以下是脚本的cProfile输出的摘录(请参阅cumtime,而不是tottime):
Python 2:
ncalls tottime percall cumtime percall filename:lineno(function)
36984 0.188 0.000 0.545 0.000 io.py:929(read)
Python 3:
36996 0.063 0.000 0.063 0.000 {method 'read' of '_io.BufferedReader' objects}
Run Code Online (Sandbox Code Playgroud)
当我打印对象时,两者都返回类似的东西io.BufferedReader,我确信它们都使用BufferedReader.
这是有问题的代码.见第28行.调用者负责设置bufstream.我用了bufstream = io.open('testfile', 'rb')
为什么BufferedReader的速度在读取文件中的单个字节方面存在如此巨大的差异,以及如何"修复"Python 2.x的问题?我正在运行Python 2.6和Python 3.1.
为了给你一个更全面的答案,你需要看到你的代码(或者更好的是,你的代码的可执行文件).
但是,可以从您的配置文件输出中收集部分答案:io.py建议"Python 2"(为避免疑问,给出实际版本号)在Python中实现BufferedReader,而_io.BufferedReader建议"Python3"在C中实现它.
最新消息:Python 2.6 io.py超过64Kb并在前面包含以下评论:
# This is a prototype; hopefully eventually some of this will be
# reimplemented in C.
Run Code Online (Sandbox Code Playgroud)
Python 2.7 io.py大约是4Kb,似乎是_io模块的薄包装.
如果您想获得2.6的解决方法的真正帮助,请显示您的代码.
Python 2.6的可能解决方法
代替:
test = io.open('test.bmp', 'rb')
做这个:
test = open('test.bmp', 'rb')
Run Code Online (Sandbox Code Playgroud)
一些粗略的时序数字,包括缺失的链接(Python 2.7):
Windows 7 Pro,32位,大约5 Mb文件,代码内容是:
while 1:
c = f.read(1)
if not c: break
2.6: io.open 20.4s, open 5.1s
2.7: io.open 3.3s, open 4.8s # io.open is better
3.1: io.open 3.6s, open 3.6s # effectively same code is used
Run Code Online (Sandbox Code Playgroud)
所以一个更好的故事似乎是这样的:总的来说,除非你有充分的理由要求你想要2.7更快,否则不要用io.open.
| 归档时间: |
|
| 查看次数: |
2244 次 |
| 最近记录: |