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

rewrite for achieving speedup

Hello Group!

I really tried hard for two hours to rewrite the following expression
(python 2.4):
--------------------------
teilnehmer = []
for r in Reisen.select(AND(Reisen.q.RESVON <= datum, Reisen.q.RESBIS
>= datum)):
for g in r.BUCHUNGEN:
for t in g.aktiveTeilnehmer:
teilnehmer.append(t)
--------------------------

to something like
--------------------------
teilnehmer = [x for x in ........]
--------------------------

Reisen is a SQLObject class, Reisen.select(...), aktiveTeilnehmer and
BUCHUNGEN all are of the type SelectResults.

unfortunately i just canīt figure it out to make it work.
i hope someone maybe can help me?

I hope to gain performance by rewriting it...

Thanks a lot for your help!

Regards, Frank

Apr 17 '07 #1
5 1043
Johnny Blonde wrote:
Hello Group!

I really tried hard for two hours to rewrite the following expression
(python 2.4):
--------------------------
teilnehmer = []
for r in Reisen.select(AND(Reisen.q.RESVON <= datum, Reisen.q.RESBIS
>= datum)):
for g in r.BUCHUNGEN:
for t in g.aktiveTeilnehmer:
teilnehmer.append(t)
--------------------------

to something like
--------------------------
teilnehmer = [x for x in ........]
--------------------------

Reisen is a SQLObject class, Reisen.select(...), aktiveTeilnehmer and
BUCHUNGEN all are of the type SelectResults.

unfortunately i just canīt figure it out to make it work.
i hope someone maybe can help me?

I hope to gain performance by rewriting it...

Thanks a lot for your help!
>>lt = [[[1,2,3], [2,3,4]], [[3,4,5], [4,5,6]]]
lf = [c for a in lt for b in a for c in b]
lf
[1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6]
>>>
Untested:

teilnehmer = [t for r in Reisen.select(AND(Reisen.q.RESVON <= datum,
reisen.q.RESBIS >= datum)) for g in r.BUCHUNGEN for t in g.aktiveTeilnehmer]

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 17 '07 #2
Steve Holden wrote:
Johnny Blonde wrote:
>Hello Group!

I really tried hard for two hours to rewrite the following expression
(python 2.4):
--------------------------
teilnehmer = []
for r in Reisen.select(AND(Reisen.q.RESVON <= datum, Reisen.q.RESBIS
>>= datum)):
for g in r.BUCHUNGEN:
for t in g.aktiveTeilnehmer:
teilnehmer.append(t)
--------------------------

to something like
--------------------------
teilnehmer = [x for x in ........]
--------------------------

Reisen is a SQLObject class, Reisen.select(...), aktiveTeilnehmer and
BUCHUNGEN all are of the type SelectResults.

unfortunately i just canīt figure it out to make it work.
i hope someone maybe can help me?

I hope to gain performance by rewriting it...

Thanks a lot for your help!
>>lt = [[[1,2,3], [2,3,4]], [[3,4,5], [4,5,6]]]
>>lf = [c for a in lt for b in a for c in b]
>>lf
[1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6]
>>>

Untested:

teilnehmer = [t for r in Reisen.select(AND(Reisen.q.RESVON <= datum,
reisen.q.RESBIS >= datum)) for g in r.BUCHUNGEN for t in
g.aktiveTeilnehmer]
Note also that you can probably get most of the speedup above by binding
the append method to a function-local name::

teilnehmer = []
append = teilnehmer.append
for r in Reisen.select(...):
for g in r.BUCHUNGEN:
for t in g.aktiveTeilnehmer:
append(t)

That's pretty much all a list comprehension is doing anyway.

STeVe
Apr 17 '07 #3
thanks steve h.,

works like this just perfectly!

steve b.:
for the next time if i cannot figure it out i will just do it like
this!

thanks a lot guys,
Frank

Apr 17 '07 #4
On Apr 17, 11:25 pm, Steven Bethard <steven.beth...@gmail.comwrote:
Steve Holden wrote:
Johnny Blonde wrote:
Hello Group!
I really tried hard for two hours to rewrite the following expression
(python 2.4):
--------------------------
teilnehmer = []
for r in Reisen.select(AND(Reisen.q.RESVON <= datum, Reisen.q.RESBIS
= datum)):
for g in r.BUCHUNGEN:
for t in g.aktiveTeilnehmer:
teilnehmer.append(t)
--------------------------
to something like
--------------------------
teilnehmer = [x for x in ........]
--------------------------
Reisen is a SQLObject class, Reisen.select(...), aktiveTeilnehmer and
BUCHUNGEN all are of the type SelectResults.
unfortunately i just canīt figure it out to make it work.
i hope someone maybe can help me?
I hope to gain performance by rewriting it...
Thanks a lot for your help!
>>lt = [[[1,2,3], [2,3,4]], [[3,4,5], [4,5,6]]]
>>lf = [c for a in lt for b in a for c in b]
>>lf
[1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6]
Untested:
teilnehmer = [t for r in Reisen.select(AND(Reisen.q.RESVON <= datum,
reisen.q.RESBIS >= datum)) for g in r.BUCHUNGEN for t in
g.aktiveTeilnehmer]

Note also that you can probably get most of the speedup above by binding
the append method to a function-local name::

teilnehmer = []
append = teilnehmer.append
for r in Reisen.select(...):
for g in r.BUCHUNGEN:
for t in g.aktiveTeilnehmer:
append(t)

That's pretty much all a list comprehension is doing anyway.

STeVe
hi steve,
why would binding to a function-local name speeds up performance?
sorry for asking such a simple question. many thanks.

Apr 18 '07 #5
Ju**********************@gmail.com <Ju*********@gmail.comwrote:
why would binding to a function-local name speeds up performance?
Like any other constant-hoisting, pulling the lookup out of the loop
speeds things up because otherwise Python must repeat the lookup each
time through the loop (Python doesn't _know_ that, for example, zip.zap
is always the same bound method object -- without potentially deep
analysis, it can't rule out that looking up zap on zip has side effects
and the like, so when you tell it to look it up, it looks it up, no ifs,
no buts).

You can measure the effect with -mtimeit ...:

brain:~ alex$ python -mtimeit -s'def f(L):
for x in xrange(1000): L.append' 'f([])'
1000 loops, best of 3: 215 usec per loop

brain:~ alex$ python -mtimeit -s'def g(L):
ap = L.append
for x in xrange(1000): ap' 'g([])'
10000 loops, best of 3: 79.1 usec per loop

note that in each function f and g I'm doing only the lookup, not the
call to the method thus looked up.

Of course, this depends on the fact that for a function to use a local
variable takes very little time indeed (since the compiler identifies
which variables are local, and transforms any use of their names into a
rapid indexing of a "local variables array", at the time the def
statement executes -- so by the time the function gets called, i.e.,
that its body executes, that execution takes advantage of this
optimization automatically performed by the Python compiler).
Alex
Apr 18 '07 #6

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

Similar topics

8
by: andrewpalumbo | last post by:
I'm trying to write some code which will split up a vector into two halves and run a method on the objects in the vector using two seperate threads. I was hoping to see a near linear speedup on an...
23
by: Mark Dickinson | last post by:
I have a simple 192-line Python script that begins with the line: dummy0 = 47 The script runs in less than 2.5 seconds. The variable dummy0 is never referenced again, directly or indirectly,...
8
by: TM | last post by:
I have a small application that displays records from an access mdb into two datagrids and am looking to see if it is possible to speedup the loadtime somehow. In my formload I am filling my...
12
by: Lars Schouw | last post by:
All, Does anyone know how much performance speedup I can expect by using 64 bit C++ / Windows XP 64 bit over the 32 bit versions? Did anyone test this under Visual Studio 2005 or Intel C++...
1
by: baroque Chou | last post by:
Thanks for the help available on msdn, I have succesful done the rewrite job. But there are 2 problems arise: 1.when I try to rewrite the url from say: www.yoursite.com/beverages.aspx to...
14
by: Stan Canepa | last post by:
This post is mostly for discussion. Why rewrite in .NET? Just a general discussion not related to any specific details. I was just looking to see what reasons developers are looking to, to help...
4
by: Galen Somerville | last post by:
My VB2005 app gets real time Heart sounds and an ECG from a USB device. I'm looking for a way to speed up the drawing of the traces on the screen. In the following code the routine GetSounds...
1
by: mazdotnet | last post by:
Hi all, I've installed the new Microsoft URL Rewrite Module for IIS 7.0 http://www.iis.net/downloads/default.aspx?tabid=34&i=1691&g=6 on both my laptop (Vista Home Premium) and my desktop...
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: 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?
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
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
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...

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.