python - cProfile没有运行

tur*_*oup 6 python cprofile

我试图使用cProfile对我的代码进行性能测试,但遗憾的是无论我如何尝试cProfile都拒绝正常运行.这是我做的:

import cProfile
cProfile.run('addNum()')  # addNum() is a very simple function that adds a bunch of 
                          # numbers into a dictionary
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

Traceback (most recent call last):
File "C:\Program Files\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "C:\Python27\Lib\cProfile.py", line 36, in run
result = prof.print_stats(sort)
File "C:\Python27\Lib\cProfile.py", line 81, in print_stats
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
File "C:\Python27\Lib\pstats.py", line 81, in __init__
self.init(arg)
File "C:\Python27\Lib\pstats.py", line 95, in init
self.load_stats(arg)
File "C:\Python27\Lib\pstats.py", line 124, in load_stats
self.__class__, arg)
TypeError: Cannot create or construct a <class pstats.Stats at 0x01AE9CA8> object from '<cProfile.Profile object at 0x01ACC470>''
Run Code Online (Sandbox Code Playgroud)

有人可以帮我调试这个,希望能提供解决方案吗?

我在Wing IDE 101 ver4.1上运行Python 2.7.3.

谢谢!!!

小智 2

这似乎是 pStats 模块的问题,而不是 cProfile 的问题。

你可以尝试做吗

import pstats
Run Code Online (Sandbox Code Playgroud)

如果提示无法导入 pstats,请尝试再次安装 python-profiler。它附带了 python 本身,但如果 pstats 不存在,那么在你的情况下可能会搞砸。

这是 Linux 上的一个简单的 apt-get,所以我假设 Windows 也有一个单独的 python-profiler 二进制文件。

希望这可以帮助!