[clug] Redirect time command output to file

Jason j.lee.nielsen at gmail.com
Wed May 26 21:42:28 MDT 2010


Hi Hal

There are three ways I profile my python code. If its a command line script like you had then I will run time (I have hit the same little problem that you did that bash overwrites the time command with its own version. Second I might just use pythons built in time.time() in a rapper function within my app and third is the python profiler.

Are you using the python profiler at all? I love it.

when I want to run the profiler (I use it on web apps) I set a cookie that calls this function instead of the processPage function its self.

def runProfiler():
   import cProfile
   page = tRequestState.getWebValue('page', '1')
   cProfile.runctx('processPage()',
                   globals(),
                   locals(),
                   '/tmp/stats_'+page)

(warning this makes it run a lot slower) then to see what I just profiled I run the following script:

#!/usr/bin/python
import sys
import pstats

args = sys.argv[1:]
page = ''
count = 30
for arg in args:
   if arg.startswith('count='):
     count = int(arg[6:])
   if arg.startswith('page='):
     page = arg[5:]

stats = pstats.Stats('/tmp/stats_'+page)
stats.strip_dirs()
stats.sort_stats('cum', 'time', 'calls')

if 'callers' in args:
   stats.print_callers(count)
elif 'callees' in args:
   stats.print_callees(count)
else:
   stats.print_stats(count)

which prints to standard out in one of three modes.

Hope some of that helps.

Jason


On Thu, 27 May 2010 13:24:20 +1000, Hal <hal.ashburner at gmail.com> wrote:

>Are there better ways of roughly profiling python database code to give you a heads up if you've done something that kills performance?
>Cheers all,
> Hal
>



More information about the linux mailing list