A question on python performance.

Joe Goldthwaite joe at goldthwaites.com
Wed Sep 26 14:26:43 EDT 2007


Hi everyone,

I'm a developer who's been using python for a couple of years.  I wrote a
fairly large application using it but I was learning the language at the
same time so it most of the code kind of sucks.

I've learned a lot since then and I've been going through my code trying to
organize it better and make better use of Python's features.  I'm still  not
an expert by any definition but I'm slowly getting better.

I've been working on a trend class that takes twelve monthly numbers and
returns a period to date, quarter to date, year to date and quarterly year
to date numbers for a specific period. This worked but I ended up with a lot
of code like this;

def getValue(trend, param, per):
   if param == 'Ptd':
     return trend.Ptd(per)
   elif param == 'Qtd':
      return trend.Qtd(per)
   elif param == 'Ytd':
      return trend.Ytd(per)
   elif param == 'YtdQ':
      return trend.YtdQ(per)

The code gets kind of wordy so I started trying to figure out how to call
them dynamically since the param type is the same as the method the
retrieves it.  I came up with this;

def getValue(trend, param, per):
   return trend.__class__.__dict__[param](trend, per)

That worked but it seems like the above line would have to do lots more
object look ups at runtime so I didn't think it would be very efficient.  I
thought maybe I could add a caller method to the trend class and I came up
with this;

class trend:
   ...
   ...
   ...
   def caller(self, param, *args):
      return self.__class__.__dict__[param](self, *args)

This simplified the getValue function to this;

def getValue(trend, param, per):
	return trend.caller(param, per)

Out of curiosity, I thought I'd do some benchmarking and see which one
performs the best. I executed three multiple times;

loop one.  Time=11.71 seconds;
    trend.Ptd(per)
    trend.Qtd(per)
    trend.Ytd(per)
    trend.YtdQ(per)

loop two. 12.107 seconds;
    trend.__class__.__dict__['Ptd'](trend, per)
    trend.__class__.__dict__['Qtd'](trend, per)
    trend.__class__.__dict__['Ytd'](trend, per)
    trend.__class__.__dict__['YtdQ'](trend, per)

loop three. 17.085 seconds;
    trend.caller('Ptd', per)
    trend.caller('Qtd', per)
    trend.caller('Ytd', per)
    trend.caller('YtdQ', per)

The first surprise was how close the first and second loops were.  I would
have thought the first loop would be much faster.  The second surprise was
how much slower the third loop was.  I know it has an extra call in there
but other than that, it's doing basically the same thing as loop two.  Is
there that much overhead in making a class method call?

Can anyone explain the differences?




More information about the Python-list mailing list