DenkzeitWiki

Suchen:

Aktuelle Änderungen Printable View Änderungen Bearbeiten

BusinessRules > C > CLanguage > CASE > ComputerAidedSoftwareEngineering > CLOS > CommonLispObjectSystem > CLR > CommonLanguageRuntime > CLanguage > CPlusPlus > CPlusPlusSprache > CS > ComputerScience > CVS > CallByReferenceOrValue > CallingPythonProfilersFromCommandLineClear Trail
Main /

Calling Python Profilers From Command Line

Profiling
PrematureOptimization
PythonOptimizations

Hacking

Speed only matters in certain critical bottlenecks.[1]


Motivation & Usage

Calling hotshot from command line withouth much hassle, just like it is possible with profile and cProfile.
The script published here is a simple wrapper around the hotshot module, which has lower impact on execution than profile or cProfile. (The script can also be used to customize the execution of those profilers (see below).) Hotshot logs profiling-information during execution of a script (or statement), post-processing that data after execution finished.

Calling hotshot, a python profiler, from command line

'Download'


hotshot-version

Just copy&paste the following code:
import hotshot, hotshot.stats
import os
import sys


if __name__ == "__main__":
    if len(sys.argv) == 0:
        sys.exit()
    
    
    if len(sys.argv) == 3:
        script = sys.argv[2]
        repetitions = int(sys.argv[1])
    else:
        script = sys.argv[1]
        repetitions = 1
    
    # stolen from profile.py
    # add dirname to python-search-path
    dirname = os.path.dirname(script)
    sys.path.insert(0, dirname)
    
    hotshot_data = "hotshot_hotcoc_stats"
    
    profile = hotshot.Profile(hotshot_data)
    # execute the script - instead of "%r" - "'%s'" would also work
    for i in range(repetitions):
        profile.run("execfile(%r)" % (script,))
    profile.close()
    
    stats = hotshot.stats.load(hotshot_data)
    # removing directory-information
    # time: time spent in function without sub-calls
    stats = stats.sort_stats('time', 'calls')
    stats.print_stats()

wrapping cProfile


Dependencies


Versioning


Edit - BackLinks - Tags - Page Hist - Print - Changes - Home - Orphans - Help

Zuletzt geändert am 03.10.2007 20:33 Uhr und seit 7. April 2005 1052 aufgerufen.