optimization question

Kristian Ovaska kristian.ovaska at helsinki.fi
Mon Aug 12 09:56:57 EDT 2002


"Matt Gerrans" <mgerrans at mindspring.com>:
>I concur.   In fact, call me crazy, but I think it is fun to first get
>something working, then chisel away at the slow parts (if necessary) and
>watch as the performance improves.   The profile module make this a
>pleasure.

I find this fun, too. And it's good to save the profile stats so you
can read afterwards exactly how the performance was improved. (If
you're into that kind of stuff.)

I'm using this script to do the profiling and printing of stats:

--- <profiler.py> ---

import profile, pstats, os, sys

TMPFILE = 'py-profile-tmp-stats'

def main(args):
    if not args:
        print 'Usage: profiler <module> [args-for-module]'
        return

    module = args[0]
    sys.argv = sys.argv[1:]
    profile.run('execfile("%s")' % module, TMPFILE)

    stats = pstats.Stats(TMPFILE)
    stats.strip_dirs()
    stats.sort_stats('time')
    stats.print_stats('^[^<].*', 25)

    if os.path.exists(TMPFILE):
        os.remove(TMPFILE)

if __name__=='__main__':
    main(sys.argv[1:])

--- </profiler.py> ---

-- 
Kristian Ovaska <kristian.ovaska at helsinki.fi>



More information about the Python-list mailing list