473,382 Members | 1,369 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,382 software developers and data experts.

"optimizing out" getattr

Hi,

I'd like to get the 'get2' function below to
perform like the 'get1' function (I've included
timeit.py results).

I'm not sure how to write 'mkget' to do achieve
this, however, except to use 'exec' - is that what
would be necessary?

Thanks in advance,
d

---
class A:
a = 1
b = 2
a = A()
labels = ('a', 'b')
def get1(x):
return (x.a, x.b)
def mkget(attrs):
def getter(x):
return tuple(getattr(x, label) for label in attrs)
return getter
get2 = mkget(labels)

# % timeit.py -s "import test" "test.get1(test.a)"
# 1000000 loops, best of 3: 0.966 usec per loop
# % timeit.py -s "import test" "test.get2(test.a)"
# 100000 loops, best of 3: 4.46 usec per loop
---

Sep 15 '05 #1
5 1654
On 14 Sep 2005 19:46:44 -0700, Daishi Harada <da****@gmail.com> wrote:
Hi,

I'd like to get the 'get2' function below to
perform like the 'get1' function (I've included
timeit.py results).


Do you have profiling results that show that a significant percentage
of your programs time is being spent inside this function?

If you don't, then you're wasting your time doing unnecessery optimisation.
--
Stephen Thorne
Development Engineer
Sep 15 '05 #2
Daishi Harada wrote:
I'd like to get the 'get2' function below to
perform like the 'get1' function (I've included
timeit.py results). labels = ('a', 'b')
def get1(x):
return (x.a, x.b)
def mkget(attrs):
def getter(x):
return tuple(getattr(x, label) for label in attrs)
return getter
get2 = mkget(labels)

# % timeit.py -s "import test" "test.get1(test.a)"
# 1000000 loops, best of 3: 0.966 usec per loop
# % timeit.py -s "import test" "test.get2(test.a)"
# 100000 loops, best of 3: 4.46 usec per loop I'm not sure how to write 'mkget' to do achieve
this, however, except to use 'exec' - is that what
would be necessary?


No, you can just sit back and wait -- for Python 2.5:

$ cat attr_tuple25.py
import operator

class A:
a = 1
b = 2

get2 = operator.attrgetter("a", "b")

def get1(x):
return x.a, x.b

$ python2.5 -m timeit -s'from attr_tuple25 import A, get1, get2' 'get1(A)'
1000000 loops, best of 3: 0.813 usec per loop
$ python2.5 -m timeit -s'from attr_tuple25 import A, get1, get2' 'get2(A)'
1000000 loops, best of 3: 0.495 usec per loop

Time till release is not included in the timings :-)

Peter

Sep 15 '05 #3
Peter Otten wrote:
Daishi Harada wrote:

I'd like to get the 'get2' function below to
perform like the 'get1' function (I've included
timeit.py results).

labels = ('a', 'b')
def get1(x):
return (x.a, x.b)
def mkget(attrs):
def getter(x):
return tuple(getattr(x, label) for label in attrs)
return getter
get2 = mkget(labels)

# % timeit.py -s "import test" "test.get1(test.a)"
# 1000000 loops, best of 3: 0.966 usec per loop
# % timeit.py -s "import test" "test.get2(test.a)"
# 100000 loops, best of 3: 4.46 usec per loop


No, you can just sit back and wait -- for Python 2.5:

$ cat attr_tuple25.py
import operator

class A:
a = 1
b = 2

get2 = operator.attrgetter("a", "b")

def get1(x):
return x.a, x.b

$ python2.5 -m timeit -s'from attr_tuple25 import A, get1, get2' 'get1(A)'
1000000 loops, best of 3: 0.813 usec per loop
$ python2.5 -m timeit -s'from attr_tuple25 import A, get1, get2' 'get2(A)'
1000000 loops, best of 3: 0.495 usec per loop


With Python 2.4 you can at least get closer to the hardcoded version:

F:\>type attr_tuple.py
import operator

class A:
a = 1
b = 2

getA = operator.attrgetter("a")
getB = operator.attrgetter("b")

def get2(x):
return getA(x), getB(x)

def get1(x):
return x.a, x.b

F:\>python -m timeit -s"from attr_tuple import A, get1, get2" "get1(A)"
1000000 loops, best of 3: 0.658 usec per loop

F:\>python -m timeit -s"from attr_tuple import A, get1, get2" "get2(A)"
1000000 loops, best of 3: 1.04 usec per loop

Kent
Sep 15 '05 #4
Peter Otten wrote:
No, you can just sit back and wait -- for Python 2.5:
Thanks for the tip;
Although for my current use
I can't target 2.5, I hadn't even
noticed the attr/itemgetter
additions to operator in 2.4,
so I appreciate the pointer
for future reference.
d
$ cat attr_tuple25.py
import operator

class A:
a = 1
b = 2

get2 = operator.attrgetter("a", "b")

def get1(x):
return x.a, x.b

$ python2.5 -m timeit -s'from attr_tuple25 import A, get1, get2' 'get1(A)'
1000000 loops, best of 3: 0.813 usec per loop
$ python2.5 -m timeit -s'from attr_tuple25 import A, get1, get2' 'get2(A)'
1000000 loops, best of 3: 0.495 usec per loop

Time till release is not included in the timings :-)

Peter


Sep 16 '05 #5
If you are including C extensions, why not crib 2.5's implementation of
operator.attrgetter? It looks like it is fairly modular.

http://cvs.sourceforge.net/viewcvs.p...les/operator.c

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFDKiBEJd01MZaTXX0RAud7AJ9WZWUf/BsAJR4KFALX+i5gLq9W6wCeIoRA
XxyI2o95VLYhsU8B9CeH0w4=
=8j9w
-----END PGP SIGNATURE-----

Sep 16 '05 #6

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

Similar topics

1
by: Robin Tucker | last post by:
My code prints out 0 for the value of charindex ( 'Hello.', '.' ). Did I miss something? I think it should print out 6!
3
by: Henne | last post by:
Hello, in VB6 we werr able to make ActiveX-DLL's (In Process Components) and ActiveX-Exe (Out Of Process Components). Out Application is build of one main application that starts other...
2
by: Berata | last post by:
Hello all, in VB6 we were able to create ActiveX-DLL's (In Process Components) and ActiveX-Exe's (Out of Process Components). We habe build up an application that exists of an main module...
4
by: J. Campbell | last post by:
From reading this forum, it is my understanding that C++ doesn't require the compiler to keep code that does not manifest itself in any way to the user. For example, in the following: { for(int...
0
by: Klemens | last post by:
what do entry's like this in db2diag.log indicate? ------------------------------------------- 2004-01-14-09.07.52.650000 Instance:WWS Node:000 PID:3512(db2syscs.exe) TID:1116 ...
11
by: John A Grandy | last post by:
asp.net 1.1 ie 6 how to lock-out the user from clicking the browser "Back" button ?
4
by: Rachel McConnell | last post by:
Hello, I have a Java web application using Hibernate to connect to a PostgreSQL backend. I am seeing the below stack trace during processing of a set of data consisting of around 1000 objects;...
7
by: John Layton | last post by:
Hi there, Is it possible to pass null to a function taking an "out" (or "ref") parameter in C#. I'd like to do something like the following (which doesn't compile of course). Thanks in advance....
16
by: saurabhnsit2001 | last post by:
The following program doesn't "seem" to print "hello-out". (Try executing it) #include <stdio.h> #include <unistd.h> int main() { while(1) { fprintf(stdout,"hello-out");
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.