我试图使用cProfile对我的代码进行性能测试,但遗憾的是,无论我如何尝试,cProfile都无法正常工作。下面是我所做的:
import cProfile
cProfile.run('addNum()') # addNum() is a very simple function that adds a bunch of
# numbers into a dictionary下面是我得到的信息:
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>''有没有人能帮我调试一下,希望能提供一个解决方案?
我在Wing IDE 101 4.1版上运行Python 2.7.3。
谢谢!
发布于 2012-07-03 14:35:14
这似乎是pStats模块的问题,而不是cProfile的问题。
你能试着做吗
import pstats如果显示不能导入pstats,则尝试重新安装python-profiler。它附带了python本身,但是如果pstats不在那里,在你的例子中可能会搞乱。
它在linux上是一个简单的apt-get,所以我假设windows也会为python-profiler提供一个单独的二进制文件。
希望这能有所帮助!
发布于 2019-02-21 18:10:53
今天我在Python 3.5.2上也遇到了同样的问题:
最终起作用的是替换我想要分析的调用,然后运行整个程序:
import cProfile
# myObject.myFunc()
cProfile.runctx('myObject.myFunc()', globals(), locals(), 'myFunc.stat')最后,在单独运行的交互式python3中,我做到了:
>>> import pstats
>>> p = pstats.Stats('myFunc.stat')
>>> p.strip_dirs().sort_stats(-1).print_stats()
Wed Feb 20 17:10:05 2019 myFunc.stat
10218759 function calls (3916491 primitive calls) in 16.519 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 16.519 16.519 <string>:1(<module>)
... the really useful stats followed here ...cProfile.runctx(...)、globals()、locals()是修复我遇到的NameError所必需的;您询问的TypeError是通过指定要存储统计数据的文件名来修复的,该文件名也可以通过普通的cProfile.run(...)获得
https://stackoverflow.com/questions/11304257
复制相似问题