Skip to content

[python] performance tuning and profiling

dsindex edited this page Jul 28, 2014 · 2 revisions

reference 1
reference 2

python -m cProfile -o cprofile.stats script.py

[cprofile_stats.py]

#!/usr/bin/env python
#-*- coding: utf8 -*-

import os
import sys
import re
from   optparse import OptionParser
import pstats

VERBOSE = 0

if __name__ == '__main__':

    parser = OptionParser()
    parser.add_option("--verbose", action="store_const", const=1, dest="verbose", help="verbose mode")
    parser.add_option("-s", "--stat", dest="stat",help="stat file", metavar="STAT")
    (options, args) = parser.parse_args()

    if options.verbose == 1 : VERBOSE = 1

    stat_path = options.stat
    if stat_path == None :
        parser.print_help()
        sys.exit(1)

    pstats.Stats(stat_path).strip_dirs().sort_stats('cumulative').print_stats()
Clone this wiki locally