gpt4 book ai didi

与 Python 3.1.x 相比,Python 3.2 非常慢

转载 作者:行者123 更新时间:2023-12-01 06:11:18 26 4
gpt4 key购买 nike

我通读了 Python 3.2 的变化,了解到它比 3.1 有了很多改进。然而,我在 3.2 上运行的零修改完全相同的代码比在 3.1.3 上运行代码慢了 10 倍以上

Python 3.2 需要六分钟才能将文件的二进制内容传输到物理设备,然后接收并在屏幕上打印出接收到的数据,而在同一台 PC 上执行完全相同的场景仅需要 30 秒,而 Python 3.1.3 则需要 30 秒.

我使用 Python 3.1.2 从头开始​​开发代码,20% 的代码使用 ctypes 通过带有 USB/PCI 设备的 Windows 驱动程序执行事务,因此我认为这种性能影响与向后兼容性没有任何关系。在我的应用程序中,我创建了四个 threading.Thread 子类实例,每个实例处理系统上的一个 PCI 或 USB 设备。我怀疑 3.2 的 ctypes 性能比以往任何时候都更差,或者我必须使用更多的 threading.Thread 才能获得我想要的多线程性能。如果有人能为我遮光,我将不胜感激

============================================

更具诊断性

我减少了要发送和接收的数据量

python 3.1.3 花费 3 秒来完成,如系统资源监视器屏幕截图所示 http://img62.imageshack.us/img62/5313/python313.png

Python 3.2 花费大约 1 分钟才能完成,如系统资源监视器屏幕截图所示 http://img197.imageshack.us/img197/8366/python32.png

我的电脑是单核 Intel P4,具有 2 GB RAM,因此我认为我们可以排除多核处理器的 GIL 因素。

我使用 yappi 来分析多次运行,以平均 3.1.3 和 3.2 上的性能结果。我发现线程和 ctypes 在 Python 3.2 上执行得很差。

这是访问Python包的标准Windows二进制文件提供的线程安全队列

on 3.1.3
name #n tsub ttot tavg
C:\Python31\lib\queue.py.qsize:86 46070 1.352867 4.234082 0.000092
C:\Python31\lib\queue.py._get:225 8305 0.012457 0.017030 0.000002
C:\Python31\lib\queue.py.get:167 8305 0.635926 1.681601 0.000202
C:\Python31\lib\queue.py._put:221 8305 0.016156 0.020717 0.000002
C:\Python31\lib\queue.py.put:124 8305 0.095320 1.138560 0.000137

on 3.2
name #n tsub ttot tavg
C:\Python32\lib\queue.py.qsize:86 252168 4.987339 15.229308 0.000060
C:\Python32\lib\queue.py._get:225 8305 0.030431 0.035152 0.000004
C:\Python32\lib\queue.py.get:167 8305 0.303126 7.898754 0.000951
C:\Python32\lib\queue.py._put:221 8305 0.015728 0.020928 0.000003
C:\Python32\lib\queue.py.put:124 8305 0.143086 0.431970 0.000052

Python 3.2 上的线程性能非常糟糕

另一个例子。该函数只是通过ctypes模块调用Windows USB驱动程序中的API并向USB设备请求16位数据

on 3.1.3
name #n tsub ttot tavg
..ckUSBInterface.py.read_register:14 1 0.000421 0.000431 0.000431
on 3.2
name #n tsub ttot tavg
..ckUSBInterface.py.read_register:14 1 0.015637 0.015651 0.015651

如您所见,在 Python 3.2 上花费的时间要差 30 倍以上

Python 3.2 对我的应用程序来说似乎是一场灾难

最佳答案

没有明显的理由说明为什么会这样。您需要对应用程序进行分析,以准确了解究竟是什么花费了这些额外的时间。

关于与 Python 3.1.x 相比,Python 3.2 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5750494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com