标签: cprofile

在分析Python脚本时,通过percall对cProfile输出进行排序

我正在使用 python -m cProfile -s calls myscript.py

python -m cProfile -s percall myscript.py 不起作用.

Python文档说"在Stats文档中查找有效的排序值.":http://docs.python.org/library/profile.html#module-cProfile,我找不到.

python profiling cprofile

61
推荐指数
1
解决办法
3万
查看次数

在使用cProfile分析的python脚本中,tottime和cumtime有什么区别?

我正在main.py使用cProfile使用以下命令分析python脚本:

python -m cProfile -s tottime main.py
Run Code Online (Sandbox Code Playgroud)

我得到的输出是(只复制粘贴输出的顶行):

10184337 function calls (10181667 primitive calls) in 13.597 seconds

Ordered by: internal time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    4.674    4.674   13.598   13.598 main.py:2(<module>)
 2142    2.964    0.001    4.663    0.002 load_aerdat3.py:61(getPacket)
  459    2.381    0.005    2.381    0.005 {waitKey}
1667989    1.170    0.000    1.170    0.000 {numpy.core.multiarray.array}

...
Run Code Online (Sandbox Code Playgroud)

(4.674)如何tottimecumtime(13.598)不同main.py,因为这个函数(即整个脚本)只被调用一次?

python cprofile

58
推荐指数
1
解决办法
1万
查看次数

在KCacheGrind中使用cProfile结果

我正在使用cProfile来配置我的Python程序.根据这个说法,我的印象是KCacheGrind可以解析并显示cProfile的输出.

但是,当我去导入文件时,KCacheGrind只会在状态栏中显示"未知文件格式"错误,并且不显示任何内容.

在我的性能分析统计数据与KCacheGrind兼容之前,我需要做些什么特别的事情吗?

...
if profile:
    import cProfile

    profileFileName = 'Profiles/pythonray_' + time.strftime('%Y%m%d_%H%M%S') + '.profile'

    profile = cProfile.Profile()
    profile.run('pilImage = camera.render(scene, samplePattern)')

    profile.dump_stats(profileFileName)
    profile.print_stats()
else:            
    pilImage = camera.render(scene, samplePattern)
...
Run Code Online (Sandbox Code Playgroud)

包版本

  • KCacheGrind 4.3.1
  • Python 2.6.2

python profiling kcachegrind cprofile

53
推荐指数
5
解决办法
2万
查看次数

cProfile将数据保存到文件会导致混乱的字符

我在名为bot4CA.py的模块上使用cProfile,因此在控制台中输入:

python -m cProfile -o thing.txt bot4CA.py
Run Code Online (Sandbox Code Playgroud)

模块运行并退出后,它会创建一个名为thing.txt的文件,当我打开它时,那里有一些信息,其余的是一堆乱七八糟的字符,而不是一个整齐有序的数据文件,这就是我想要的.有没有人知道如何使用cProfile并最终得到整齐有序的数据表,比如在命令行中正常使用它,除了在文件中?以下是.txt文件中某些数据的示例:

{(   s)   build\bdist.win32\egg\colorama\winterm.pyi'   t      reset_all(   i   i   gpàÂs% ?geOÙHÌœE?{(   s-   build\bdist.win32\egg\colorama\ansitowin32.pyi¥
Run Code Online (Sandbox Code Playgroud)

我真正想要的是当您调用cProfile.run()时会发生什么,这会导致打印整齐有序的表格,显示所有功能的执行时间,而不是打印,保存在文件中,因为此程序相当大并且运行很多功能.

python command-line profiling file cprofile

43
推荐指数
3
解决办法
3万
查看次数

Python多进程概要分析

我正在努力弄清楚如何分析一个简单的多进程python脚本

import multiprocessing
import cProfile
import time
def worker(num):
    time.sleep(3)
    print 'Worker:', num

if __name__ == '__main__':
    for i in range(5):
        p = multiprocessing.Process(target=worker, args=(i,))
        cProfile.run('p.start()', 'prof%d.prof' %i)
Run Code Online (Sandbox Code Playgroud)

我正在启动5个进程,因此cProfile会生成5个不同的文件.在每个内部我想看到我的方法'worker'运行大约需要3秒钟,但我只看到'start'method内部正在发生的事情.

如果有人能向我解释,我将不胜感激.

更新:基于已接受答案的工作示例:

import multiprocessing
import cProfile
import time
def test(num):
    time.sleep(3)
    print 'Worker:', num

def worker(num):
    cProfile.runctx('test(num)', globals(), locals(), 'prof%d.prof' %num)


if __name__ == '__main__':
    for i in range(5):
        p = multiprocessing.Process(target=worker, args=(i,))
        p.start()
Run Code Online (Sandbox Code Playgroud)

python multiprocessing cprofile

42
推荐指数
2
解决办法
1万
查看次数

使用cProfile在Python中分析类的方法?

我想使用cProfile在Python中分析函数的方法.我尝试了以下方法:

import cProfile as profile

# Inside the class method...
profile.run("self.myMethod()", "output_file")
Run Code Online (Sandbox Code Playgroud)

但它不起作用.如何用"run"调用self.method?

python profiler cprofile

34
推荐指数
3
解决办法
2万
查看次数

Python从cProfile获得有意义的结果

我在一个文件中有一个Python脚本,运行时间超过30秒.我试图描述它,因为我想大幅削减这一次.

我试图使用脚本来编写脚本cProfile,但基本上所有它似乎都在告诉我是的,主脚本需要很长时间才能运行,但是没有给出我期望的那种故障.在终端,我键入如下内容:

cat my_script_input.txt | python -m cProfile -s time my_script.py
Run Code Online (Sandbox Code Playgroud)

我得到的结果是:

<my_script_output>
             683121 function calls (682169 primitive calls) in 32.133 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1   31.980   31.980   32.133   32.133 my_script.py:18(<module>)
   121089    0.050    0.000    0.050    0.000 {method 'split' of 'str' objects}
   121090    0.038    0.000    0.049    0.000 fileinput.py:243(next)
        2    0.027    0.014    0.036    0.018 {method 'sort' of 'list' objects}
   121089    0.009    0.000    0.009    0.000 {method 'strip' of 'str' objects}
   201534    0.009    0.000    0.009 …
Run Code Online (Sandbox Code Playgroud)

python cprofile

33
推荐指数
1
解决办法
3万
查看次数

如何在python中使用urllib2加速获取页面?

我有一个脚本,可以获取多个网页并解析信息.

(可以在http://bluedevilbooks.com/search/?DEPT=MATH&CLASS=103&SEC=01上看到一个例子)

我在它上面运行了cProfile,而且正如我所假设的那样,urlopen占用了大量的时间.有没有办法更快地获取页面?或者一种方法一次获取几个页面?我会做最简单的事情,因为我是python和web开发的新手.

提前致谢!:)

更新:我有一个调用的函数fetchURLs(),我用它来制作一个我需要的URL数组,如下所示urls = fetchURLS().这些URL是来自亚马逊和eBay API的所有XML文件(这让我很困惑,为什么加载需要这么长时间,也许我的虚拟主机很慢?)

我需要做的是加载每个URL,读取每个页面,并将该数据发送到脚本的另一部分,该部分将解析和显示数据.

请注意,在获取所有页面之前,我无法执行后一部分,这就是我的问题所在.

另外,我相信我的主机一次限制我25个进程,所以服务器上最简单的任何东西都会很好:)


这是时间:

Sun Aug 15 20:51:22 2010    prof

         211352 function calls (209292 primitive calls) in 22.254 CPU seconds

   Ordered by: internal time
   List reduced from 404 to 10 due to restriction <10>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       10   18.056    1.806   18.056    1.806 {_socket.getaddrinfo}
     4991    2.730    0.001    2.730    0.001 {method 'recv' of '_socket.socket' objects}
       10    0.490    0.049    0.490    0.049 {method 'connect' of '_socket.socket' objects} …
Run Code Online (Sandbox Code Playgroud)

python time urllib2 cprofile urlopen

27
推荐指数
3
解决办法
2万
查看次数

我可以通过pytest测试运行line_profiler吗?

我已经确定了一些长期运行的pytest测试

py.test --durations=10
Run Code Online (Sandbox Code Playgroud)

我想用line_profiler或cprofile之类的东西来测试其中一个测试.我真的想从测试本身获取配置文件数据,因为pytest设置或拆除很可能是慢速的一部分.

但是考虑到line_profiler或cprofile通常是如何涉及的,我不清楚如何使它们与pytest一起工作.

python cprofile pytest

24
推荐指数
3
解决办法
5590
查看次数

为什么cProfile模块不能与unittest一起使用?

我想使用cProfile模块来分析我的单元测试.但是当我跑步的时候

python -mcProfile mytest.py
Run Code Online (Sandbox Code Playgroud)

我在'0.000秒'得到了'Ran 0测试'.这是mytest.py的源代码

import unittest

class TestBasic(unittest.TestCase):
    def testFoo(self):
        assert True == True

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

我也测试了其他更复杂的单元测试.如果我用cProfile运行它,总是得到'Ran 0 tests'.请帮忙.

更新:我的操作系统是MacOS 10.7,内置python 2.7.相同的代码在ubuntu上正常工作.

python macos unit-testing cprofile

22
推荐指数
3
解决办法
4373
查看次数