473,473 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

profile stats interpretation

I've been working on optimizing some code (yes, it really needs it--it's
too slow ;) -- decided to use hotshot. I'm assuming things about the
output of hotshot.stats that I want to verify before I make decisions
off of them.

Here's an example of output I'm getting. I coded the same function 3
different ways--it's basically a type coercer. Each method results in
different stats (for the same request):

ncalls tottime percall cumtime percall filename:lineno(function)
17582 0.670 0.000 1.428 0.000 logic.py:133(coerce)

ncalls tottime percall cumtime percall filename:lineno(function)
17582 0.509 0.000 1.829 0.000 logic.py:133(coerce)

ncalls tottime percall cumtime percall filename:lineno(function)
17582 0.604 0.000 1.202 0.000 logic.py:133(coerce)

The question is: which of these three should I keep? Is "tottime" the
time of the code within coerce(), without regard to functions called
from coerce()? If so, it seems method #2 is superior. Finally, why might
#3 have a much lower cumtime but higher tottime than #2, given that I
didn't change any other code? Hmmm.
FWIW, here's the function.

Method #1:

def coerce(self, value, valuetype=None):
if valuetype is None:
valuetype = type(value)
try:
xform = self.processors[valuetype]
except KeyError:
xform = self.default_processor
return xform(value)

Method #2:

def coerce(self, value, valuetype=None):
if valuetype is None:
valuetype = type(value)
xform = self.processors.get(valuetype, self.default_processor)
return xform(value)

Method #3:

def coerce(self, value, valuetype=None):
if valuetype is None:
valuetype = type(value)
if valuetype in self.processors:
xform = self.processors[valuetype]
else:
xform = self.default_processor
return xform(value)
Any advice would be appreciated.
Robert Brewer
MIS
Amor Ministries
fu******@amor.org
Jul 18 '05 #1
1 1970
Robert Brewer wrote:
I've been working on optimizing some code (yes, it really needs it--it's
too slow ;) -- decided to use hotshot. I'm assuming things about the
output of hotshot.stats that I want to verify before I make decisions
off of them.

Here's an example of output I'm getting. I coded the same function 3
different ways--it's basically a type coercer. Each method results in
different stats (for the same request):

ncalls tottime percall cumtime percall filename:lineno(function)
17582 0.670 0.000 1.428 0.000 logic.py:133(coerce)

ncalls tottime percall cumtime percall filename:lineno(function)
17582 0.509 0.000 1.829 0.000 logic.py:133(coerce)

ncalls tottime percall cumtime percall filename:lineno(function)
17582 0.604 0.000 1.202 0.000 logic.py:133(coerce)

The question is: which of these three should I keep? Is "tottime" the
time of the code within coerce(), without regard to functions called
from coerce()? If so, it seems method #2 is superior. Finally, why might
Judging from http://docs.python.org/lib/module-profile.html I would think
so. However, You should always pick a function based on its cumulated time.
If tottime is low in relation to cumtime that would merely be a hint that
you should rather optimize the called functions (not an option in your
example) or replace them which may require other changes to the caller
(which is what you did).
#3 have a much lower cumtime but higher tottime than #2, given that I
didn't change any other code? Hmmm.
FWIW, here's the function.

Method #1:

def coerce(self, value, valuetype=None):
if valuetype is None:
Instead of
valuetype = type(value)
valuetype = value.__class__

might work, too.
try:
xform = self.processors[valuetype]
except KeyError:
xform = self.default_processor
# assuming the normal type/value ratio the following
# line could drastically increase your hit rate.
self.processors[valuetype] = xform
return xform(value)

Method #2:

def coerce(self, value, valuetype=None):
if valuetype is None:
valuetype = type(value)
xform = self.processors.get(valuetype, self.default_processor)
return xform(value)

Method #3:

def coerce(self, value, valuetype=None):
if valuetype is None:
valuetype = type(value)
if valuetype in self.processors:
xform = self.processors[valuetype]
else:
xform = self.default_processor
return xform(value)
Any advice would be appreciated.


Once you have spotted a single slow function in heavy usage you can resort
to a micro-optimization tool like timeit. A function is "slow" when your
app spends a long time in it and the subroutine calls are necessary and
cannot be optimized themselves. Then picking the variant with the smallest
cumulated time should be a no-brainer.

Put another way, hotshot is useful to find the hotspots, i. e. the functions
that need optimizing, but not the optimization itself.

(Silly disclaimer: I have not yet worked with hotshot, so take this with
caution - the experts all seem to be redecorating :-)

Peter

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lee Harr | last post by:
Is there an easy way to sort the stats returned from the hotshot.Profile() by the percall column?
1
by: kyo guan | last post by:
Hi All: Python 2.4.1 (#2, May 5 2005, 11:32:06) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import hotshot,hotshot.stats Traceback (most recent call...
0
by: Brian Cole | last post by:
I'm profiling some code that screens a large database. The algorithm efficiency is heavily variable, based upon the different records in the database. In order to get a sense of the best place to...
2
by: scsoce | last post by:
I want to profile a function which has some lines of statement. It seem that profile module only report function's stats instead of every line of code, how can i profile every line of code?...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.