Home Download Buy Blog Forum Support

Timing Python execution

Timing Python execution

Postby agibsonsw on Sun Apr 29, 2012 12:04 am

Hello. Is there a reasonably straight-forward way, or library/module, that will enable me to check how long is spent in each procedure when my program is run? That is, without my having to set up timers within every procedure.

I should say that my program runs as an ST-TextCommand, as this may present an additional "hurdle". Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm


Re: Timing Python execution

Postby agibsonsw on Sun Apr 29, 2012 2:48 pm

quarnster wrote:http://docs.python.org/library/profile.html


Thank for responding. I'll have to investigate further, as I'm not sure how to utilise this for an ST TextCommand. That is, am I able to create a separate command that issues the single instruction to 'cProfile.run('MyTextCommand') :?: Praticularly as MyTextCommand is a class.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Timing Python execution

Postby agibsonsw on Sun Apr 29, 2012 3:33 pm

Actually, no worries ;)

I just used time.time() in a couple of spots. It takes 1.35 secs to run my entire procedure, but 1.4 to then open a browser window.

Not the kind of stats I'm going to worry about: Python is pretty impressive :D
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Timing Python execution

Postby quarnster on Sun Apr 29, 2012 6:47 pm

cProfile is great if you have a deep call stack though as it separates the time taken for each method called.

Just rename your current "run" method to "run_real" and create a new run method that does cProfile.run("self.run_real(whatever)", locals(), globals()) or similar.
quarnster
 
Posts: 389
Joined: Tue Nov 29, 2011 11:34 am

Re: Timing Python execution

Postby agibsonsw on Sun Apr 29, 2012 6:59 pm

quarnster wrote:cProfile is great if you have a deep call stack though as it separates the time taken for each method called.

Just rename your current "run" method to "run_real" and create a new run method that does cProfile.run("self.run_real(whatever)", locals(), globals()) or similar.


Thank you. Trying to get this to work:
Code: Select all
   def run(self, edit, numbers):
      cProfile.run("self.real_run(edit, numbers)", locals(), globals())


But receive error "TypeError: coercing to Unicode: need string or buffer, dict found". How can I pass edit (if necessary) and my "numbers" == true/false argument to the .run call please?

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Timing Python execution

Postby quarnster on Sun Apr 29, 2012 7:13 pm

Apologies, use cProfile.runctx rather than "run"
quarnster
 
Posts: 389
Joined: Tue Nov 29, 2011 11:34 am

Re: Timing Python execution

Postby agibsonsw on Sun Apr 29, 2012 7:40 pm

quarnster wrote:Apologies, use cProfile.runctx rather than "run"


Hi, thank you. Got it working (it's globals first :) with runctx ). Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm


Return to Technical Support

Who is online

Users browsing this forum: Yahoo [Bot] and 38 guests