[Tutor] script too slow

Paul Tremblay phthenry@earthlink.net
Mon Feb 24 23:12:01 2003


On Mon, Feb 24, 2003 at 06:01:58PM -0500, Paul Tremblay wrote:

Okay, answering my own email. Duh! It only requires that I do this to my
code:

if __name__=='__main__':
        import profile
	profile.run('Handle_Main(),', '/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()

I could even make this prettier, by doing something like:

if __name__=='__main__':
	if debug:
		#run the profilier
	else:
		Handle_Main()

But I get the idea.

I won't have time to explain everything I found out by that very, very
nifty profile module until tomorrow or maybe even the next day. But let
me say that it is *not* the dictionaries that were eating up the time.
There was a small function to split the token into the main part and the
number part, and apparently this little function, which uses a regular
expession, is a huge time hog. It is taking around 30 percent of the
total time!

Thanks for all the help so far. The profile module is really going to
help me optimize my script.

Paul

> From: Paul Tremblay <phthenry@earthlink.net>
> 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*
> ************************
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 

************************
*Paul Tremblay         *
*phthenry@earthlink.net*
************************