[Tutor] script too slow
Paul Tremblay
phthenry@earthlink.net
Mon Feb 24 18:07:18 2003
On Mon, Feb 24, 2003 at 11:07:34AM +0100, Michael Janssen wrote:
>
> Have you already "profiled" your code?
> # in script:
> import profile
> profile.run('<function to profile: func(arg)>', '<file to write to>')
>
>
> # then in interpreter:
> import pstats
> p = pstats.Stats('<file written to>')
>
> p.print_stats()
>
> # nicer output
> p.strip_dirs().sort_stats('time').print_stats()
>
> This possibly gives you a hint what kind of operations takes the time.
>
I've been struggling to profile my code for the last 1 1/2 hours with
no luck. If I have a simple function called 'print_hello', then this
code works:
import profile
profile.run('test_hello()', '/home/paul/paultemp/profile_args.data')
import pstats
p = pstats.Stats('/home/paul/paultemp/profile_args.data')
p.strip_dirs().sort_stats('time').print_stats()
However, if my code is part of a class, which is also part of a module,
then I don't know how to proceed:
class ProcessTokens:
def __init__(self):
profile.run('self.process_cw()', '/home/paul/paultemp/profile_args.data')
...
File "<string>", line 1, in ?
NameError: name 'self' is not defined
Where to I put the 'profile.run statment? Do I include it as part of the
regular script, or do I write a completely different script to profile
things? I have looked at the library documentation with no luck.
Thanks
Paul
--
************************
*Paul Tremblay *
*phthenry@earthlink.net*
************************