profile stats interpretation

Robert Brewer fumanchu at amor.org
Mon Aug 9 02:46:37 EDT 2004


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
fumanchu at amor.org



More information about the Python-list mailing list