473,411 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

A question on python performance.

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?

Sep 26 '07 #1
3 1212
On Sep 26, 2:26 pm, "Joe Goldthwaite" <j...@goldthwaites.comwrote:
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?
Makes perfect sense to me! Think about it:

method 1: looks up the method directly from the object (fastest)
method 2: looks up __class__, then looks up __dict__, then gets the
element from __dict__
method 3: looks up caller, looks up __class__, looks up __dict__, gets
element from __dict__

To get the element directly from the object (method 1), Python has to
internally check __class__.__dict__[element], which shows why method 1
and method 2 are nearly the same speed. The last version has to look
up caller in addition to the process described by method 2.

The best way to do what you are doing:

getattr(self, param)(self, *args)

Sep 26 '07 #2
On Sep 26, 7:26 pm, "Joe Goldthwaite" <j...@goldthwaites.comwrote:
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)
You're calling a function (getValue) that just calls a method of trend
(caller), that just calls another method of trend (Ptd or Qtd or ...).
You can skip all these steps, and just call the method yourself: the
code that calls getValue(trend, param, per) replace with
trend.<something>(per) if you're calling getValue with a static value
for param, or getattr(trend, param)(per) if param is dynamic.

--
Paul Hankin

Sep 26 '07 #3
Joe Goldthwaite a écrit :
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 first obvious simplification is to replace this with:

def getValue(trend, param, per):
meth = getattr(trend, param)
return meth(per)

The main difference is that it will raise (instead of returning None) if
param is not the name of a method of trend.

The second simplification is to either get rid of getValue() (which is
mostly useless).
The code gets kind of wordy
indeed
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)
Note that this is not strictly equivalent:
class Parent(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return "<%s %s>" % (self.__class__.__name__, self.name)

def dothis(self):
return "parent.dothis %s" % self

class Child(Parent):
def dothis(self):
return "Child.dothis %s" % self

class OtherChild(Parent): pass

def dothat(obj):
return "dothat %s" % obj

p = Parent('p')
c1 = Child('c1')
c2 = Child('c2')
c2.dothis = dothat.__get__(c2, type(c2))
o1 = OtherChild('o1');
o2 = OtherChild('o2');
o2.dothis = dothat.__get__(o2, type(o2))

for obj in p, c1, c2, o1, o2:
print "obj : %s" % obj
print "direct call :"
print obj.dothis()
print "via obj.__class__.__dict__ :"
try:
print obj.__class__.__dict__["dothis"](obj)
except KeyError, e:
print "oops - key error: %s" % e
print

=>
obj : <Parent p>
direct call :
parent.dothis <Parent p>
via obj.__class__.__dict__ :
parent.dothis <Parent p>

obj : <Child c1>
direct call :
Child.dothis <Child c1>
via obj.__class__.__dict__ :
Child.dothis <Child c1>

obj : <Child c2>
direct call :
dothat <Child c2>
via obj.__class__.__dict__ :
Child.dothis <Child c2>

obj : <OtherChild o1>
direct call :
parent.dothis <OtherChild o1>
via obj.__class__.__dict__ :
oops - key error: 'dothis'

obj : <OtherChild o2>
direct call :
dothat <OtherChild o2>
via obj.__class__.__dict__ :
oops - key error: 'dothis'
IOW, direct access to obj.__class__.__dict__ bypasses both inheritence
and per-instance overriding.
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)
Err... It actually means *more* lookup and function calls - and still
fails to behave correctly wrt/ polymorphic dispatch.
Sep 27 '07 #4

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

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
8
by: Sridhar R | last post by:
Hi, I am a little experienced python programmer (2 months). I am somewhat experienced in C/C++. I am planning (now in design stage) to write an IDE in python. The IDE will not be a simple...
14
by: Wolfgang Keller | last post by:
Hello, as a non-developer I am currently participating in an industrial "research" project to develop a so-called "web application". This application serves at the same time as middleware to...
47
by: Michael Scarlett | last post by:
There is an amazing article by paul graham about python, and an even better discussion about it on slashdot. The reason I point this out, is the more I read both articles, the more I realised how...
53
by: Krystian | last post by:
Hi are there any future perspectives for Python to be as fast as java? i would like to use Python as a language for writing games. best regards krystian
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 375 open ( -3) / 3264 closed (+26) / 3639 total (+23) Bugs : 910 open ( +3) / 5851 closed (+20) / 6761 total (+23) RFE : 217 open...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
18
by: Jens | last post by:
I'm starting a project in data mining, and I'm considering Python and Java as possible platforms. I'm conserned by performance. Most benchmarks report that Java is about 10-15 times faster than...
71
by: Jack | last post by:
I understand that the standard Python distribution is considered the C-Python. Howerver, the current C-Python is really a combination of C and Python implementation. There are about 2000 Python...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.