Sublime Forum

Timing Python execution

#1

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.

0 Likes

#2

docs.python.org/library/profile.html

0 Likes

#3

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’) :question: Praticularly as MyTextCommand is a class.

0 Likes

#4

Actually, no worries :wink:

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 :smiley:

0 Likes

#5

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.

0 Likes

#6

[quote=“quarnster”]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.[/quote]

Thank you. Trying to get this to work:

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.

0 Likes

#7

Apologies, use cProfile.runctx rather than “run”

0 Likes

#8

Hi, thank you. Got it working (it’s globals first :smile: with runctx ). Andy.

0 Likes