1

我通读了 Python 3.2 的更改,了解到它比 3.1 做了很多改进。但是,我在 3.2 上运行零修改的完全相同的代码比在 3.1.3 上运行代码慢 10 倍以上

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

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

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

更具诊断性

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

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

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

我的 PC 是具有 2 GB RAM 的单核 Intel P4,所以我认为我们可以排除多核处理器的 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 对我的应用程序来说似乎是一场灾难

4

1 回答 1

2

没有明显的理由为什么会这样。您需要对应用程序进行概要分析,以确切了解需要额外时间的原因。

于 2011-04-21T23:22:19.547 回答