문제

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)
...

패키지 버전

  • Kcachegrind 4.3.1
  • 파이썬 2.6.2
도움이 되었습니까?

해결책 3

호출 된 외부 모듈을 사용하여 수행 할 수 있습니다 lscallproftree

이 기사는 다음과 같은 방법을 설명합니다. Cherrypy -Cachegrind

결과 코드가 그렇게 보입니다.

...
if profile:
    import cProfile
    import lsprofcalltree

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

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

    kProfile = lsprofcalltree.KCacheGrind(profile)

    kFile = open (profileFileName, 'w+')
    kProfile.output(kFile)
    kFile.close()

    profile.print_stats()    
else:            
    pilImage = camera.render(scene, samplePattern)
...

외부 (예 : Python) 모듈이 필요하지 않은이 작업을 수행하는 방법을 알고 있다면 여전히 그 소식에 대해 매우 관심이 있습니다.

다른 팁

Cprofile을 사용하면 별도의 프로파일 링 스크립트를 작성하지 않고도 기존 프로그램을 프로필 할 수도 있습니다. 프로파일 러와 함께 프로그램을 실행하십시오

python -m cProfile -o profile_data.pyprof script_to_profile.py

Kcachegrind에서 Kcachegrind에서 데이터를 자동으로 열리는 Pyprof2Calltree를 사용하여 Kcachegrind에서 프로필 데이터를 열어

pyprof2calltree -i profile_data.pyprof -k

예를 들어 전체 파스터 서버 및 웹 앱 프로파일 링은 다음과 같이 수행됩니다.

python -m cProfile -o pyprof.out `which paster` serve development.ini

pyprof2calltree는 Easy_install과 함께 설치할 수 있습니다.

당신은 사용할 수 있습니다 profilestats.profile 데코레이터 ($ pip install profilestats) - 간단한 포장지 pyprof2calltree 모듈 (브랜드 변경 lsprofcalltree.py):

from profilestats import profile

@profile
def func():
    # do something here

스크립트는 평소처럼 실행할 수 있습니다. profilestats 두 파일을 만듭니다. cachegrind.out.profilestats 그리고 profilestats.prof Kcachegrind-호환 및 cprofile 형식에서.

실제로하려고하는 것은 속도를 위해 코드의 어떤 부분을 최적화 할 수 있는지를보고 디버거에서 무작위로 일시 중지 할 수 있습니다. 이 방법은 작동합니다. 놀랍지 만 많은 스택 샷이 필요하지 않습니다.

코드를 프로파일 링하고 Kcachegrind/Qcachegrind에서 결과를 시각화하는 3 가지 다른 방법 :

i- cprofile

1- ipython의 프로파일 myfunc ()

import cProfile
filename = 'filename.prof'
cProfile.run('myfunc()', filename)

2- 파일을 쉘의 사용 가능한 kcachegrind 파일로 변환

sudo pip install pyprof2calltree
pyprof2calltree -i filename.prof -o callgrind.filename.prof

3- kcachegrind의 Callgrind.filename.prof를 엽니 다

II- 임베디드 CPROFILE

1- 프로파일 코드의 몇 줄.

import cProfile
filename = 'filename.prof'
pr = cProfile.Profile()
pr.enable()
# ... lines to profile ...
pr.disable()
pr.dump_stats(filename)

2- 파일을 쉘의 사용 가능한 kcachegrind 파일로 변환

sudo pip install pyprof2calltree
pyprof2calltree -i filename.prof -o callgrind.filename.prof

3- kcachegrind의 Callgrind.filename.prof를 엽니 다

III -YAPPI

1- ipython 또는 코드에서 프로파일 myfunc ()

import yappi
filename = 'callgrind.filename.prof'
yappi.set_clock_type('cpu')
yappi.start(builtins=True)
myfunc()
stats = yappi.get_func_stats()
stats.save(filename, type='callgrind')

2- kcachegrind의 Callgrind.filename.prof를 엽니 다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top