473,400 Members | 2,145 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,400 software developers and data experts.

built in zip function speed

I hope I am not being too ignorant :p but here goes... my boss has
written a bit of python code and asked me to speed it up for him...
I've reduced the run time from around 20 minutes to 13 (not bad I think
;) to speed it up further I asked him to replace a loop like this:-
index = 0

for element in a:
av = a[index]
bv = b[index]
cv = c[index]
dv = d[index]
avbv = (av-bv) * (av-bv)
diff = cv - dv
e.append(diff - avbv)
index = index + 1

(where a, b, c and d are 200,000 element float arrays)
to use the built in zip function.. it would seem made for this problem!

for av, bv, cv, dv in zip(a, b, c, d):
avbv = (av-bv) * (av - bv)
diff = cv - dv
e.append(diff - avbv)

however this seems to run much slower than *I* thought it would
(and in fact slower than slicing) I guess what I am asking is.. would
you expect this?

full code listing (I hope I have made a very obvious error):-

import array
import time
a = array.array("f")
b = array.array("f")
c = array.array("f")
d = array.array("f")
e = array.array("f")

for value in xrange(1, 200000, 1):
a.append(float(value))
b.append(float(value))
c.append(float(value))
d.append(float(value))

start = time.time()

index = 0

for element in a:
av = a[index]
bv = b[index]
cv = c[index]
dv = d[index]
avbv = (av-bv) * (av-bv)
diff = cv - dv
e.append(diff - avbv)
index = index + 1

end0 = time.time()

print end0-start
e = array.array("f")
for av, bv, cv, dv in zip(a, b, c, d):
avbv = (av-bv) * (av - bv)
diff = cv - dv
e.append(diff - avbv)

end1 = time.time()

print end1-end0

e = array.array("f")

## just for a laugh my own zip function
## the joke is it runs faster than built in zip ??

def myzip(*args):
index = 0
for elem in args[0]:
zipper = []
for arg in args:
zipper.append(arg[index])
index = index +1
yield zipper


for av, bv, cv, dv in myzip(a, b, c, d):
avbv = (av-bv) * (av - bv)
diff = cv - dv
e.append(diff - avbv)

end2 = time.time()

print end2-end1

timings from 4 million element input array

slice:
8.77999997139

zip():
36.5759999752

myzip():
12.1449999809

Jul 4 '06 #1
12 8320
itertools.izip is usually faster than zip. You can try that.

Jul 4 '06 #2

Rune Strand wrote:
itertools.izip is usually faster than zip. You can try that.

Thanks very much

timing for itertools.izip

for av, bv, cv, dv in itertools.izip(a, b, c, d):
avbv = (av-bv) * (av - bv)
diff = cv - dv
e.append(diff - avbv)
on a 4 million element aray:

slice:
8.06299996376

built in zip:
36.5169999599

myzip:
12.0320000648

izip:
5.76499986649
so fastest overall

Jul 4 '06 #3
Rune Strand wrote:
itertools.izip is usually faster than zip. You can try that.

Thanks very much

timing for itertools.izip

for av, bv, cv, dv in itertools.izip(a, b, c, d):
avbv = (av-bv) * (av - bv)
diff = cv - dv
e.append(diff - avbv)
on a 4 million element aray:

slice:
8.06299996376

built in zip:
36.5169999599

myzip:
12.0320000648

izip:
5.76499986649
so fastest overall

Jul 4 '06 #4
On Tue, 04 Jul 2006 07:18:29 -0700, ma***********@gmail.com wrote:
I hope I am not being too ignorant :p but here goes... my boss has
written a bit of python code and asked me to speed it up for him...
I've reduced the run time from around 20 minutes to 13 (not bad I think
;) to speed it up further I asked him to replace a loop like this:-
index = 0

for element in a:
av = a[index]
bv = b[index]
cv = c[index]
dv = d[index]
avbv = (av-bv) * (av-bv)
diff = cv - dv
e.append(diff - avbv)
index = index + 1
This is, I think, a good case for an old-fashioned for-with-index loop:

for i in len(a):
e.append(c[i] - d[i] - (a[i] - b[i])**2)

Python doesn't optimize away lines of code -- you have to do it yourself.
Every line of Python code takes a bit of time to execute. My version uses
34 lines disassembled; yours takes 60 lines, almost twice as much code.

(See the dis module for further details.)

It's too much to hope that my code will be twice as fast as yours, but it
should be a little faster.
(where a, b, c and d are 200,000 element float arrays)
to use the built in zip function.. it would seem made for this problem!

for av, bv, cv, dv in zip(a, b, c, d):
avbv = (av-bv) * (av - bv)
diff = cv - dv
e.append(diff - avbv)

however this seems to run much slower than *I* thought it would
(and in fact slower than slicing) I guess what I am asking is.. would
you expect this?
Yes. zip() makes a copy of your data. It's going to take some time to copy
4 * 200,000 floats into one rather large list. That list is an ordinary
Python list of objects, not an array of bytes like the array module
uses. That means zip has to convert every one of those 800,000 floats
into rich Python float objects. This won't matter for small sets of data,
but with 800,000 of them, it all adds up.
--
Steven.

Jul 4 '06 #5
ma***********@gmail.com wrote:
## just for a laugh my own zip function
## the joke is it runs faster than built in zip ??
since it doesn't do the same thing, it's not a very good joke.
def myzip(*args):
index = 0
for elem in args[0]:
zipper = []
for arg in args:
zipper.append(arg[index])
index = index +1
yield zipper
</F>

Jul 4 '06 #6
so fastest overall
you may experience speed-ups by using

from itertools import izip

and just use izip() instead to avoid the module namespace lookup. The
same applies for the list.append() methods. If you're appending some
million times

a_list = []
a_list_append = a_list.append
a_list_append(value)

will be faster than

a_list.append(value)

but not much.

Jul 4 '06 #7
Steven D'Aprano wrote:
On Tue, 04 Jul 2006 07:18:29 -0700, ma***********@gmail.com wrote:
I hope I am not being too ignorant :p but here goes... my boss has
written a bit of python code and asked me to speed it up for him...
I've reduced the run time from around 20 minutes to 13 (not bad I think
;) to speed it up further I asked him to replace a loop like this:-
index = 0

for element in a:
av = a[index]
bv = b[index]
cv = c[index]
dv = d[index]
avbv = (av-bv) * (av-bv)
diff = cv - dv
e.append(diff - avbv)
index = index + 1

This is, I think, a good case for an old-fashioned for-with-index loop:

for i in len(a):
e.append(c[i] - d[i] - (a[i] - b[i])**2)

Python doesn't optimize away lines of code -- you have to do it yourself.
Every line of Python code takes a bit of time to execute. My version uses
34 lines disassembled; yours takes 60 lines, almost twice as much code.

(See the dis module for further details.)

It's too much to hope that my code will be twice as fast as yours, but it
should be a little faster.
indeed thanks very much :)

my tests on 4 million:-

slice (original):
7.73399996758

built in zip:
36.7350001335

izip:
5.98399996758

Steven slice:
4.96899986267
so overall fastest so far


>
(where a, b, c and d are 200,000 element float arrays)
to use the built in zip function.. it would seem made for this problem!

for av, bv, cv, dv in zip(a, b, c, d):
avbv = (av-bv) * (av - bv)
diff = cv - dv
e.append(diff - avbv)

however this seems to run much slower than *I* thought it would
(and in fact slower than slicing) I guess what I am asking is.. would
you expect this?

Yes. zip() makes a copy of your data. It's going to take some time to copy
4 * 200,000 floats into one rather large list. That list is an ordinary
Python list of objects, not an array of bytes like the array module
uses. That means zip has to convert every one of those 800,000 floats
into rich Python float objects. This won't matter for small sets of data,
but with 800,000 of them, it all adds up.

I was beginning to suspect this was the case (I opened windows task
manager and noticed the memory usage) thanks for explaining it to me.

--
Steven.
Jul 4 '06 #8

Fredrik Lundh wrote:
ma***********@gmail.com wrote:
## just for a laugh my own zip function
## the joke is it runs faster than built in zip ??

since it doesn't do the same thing, it's not a very good joke.
def myzip(*args):
index = 0
for elem in args[0]:
zipper = []
for arg in args:
zipper.append(arg[index])
index = index +1
yield zipper

</F>
indeed, the joke is on me ;) I thanks for pointing it out

Jul 4 '06 #9
ma***********@gmail.com:

Using Python you can do:

# Data:
l_a = [1.1, 1.2]
l_b = [2.1, 2.2]
l_c = [3.1, 3.2]
l_d = [5.1, 4.2]

from itertools import izip
l_e = [(c-d) - (a-b)*(a-b) for a,b,c,d in izip(l_a, l_b, l_c, l_d)]
print l_e

With psyco + the standard module array you can probably go quite fast,
Psyco regognizes those arrays and speeds them a lot.

But with something like this you can probably go faster:

from numarray import array
arr_a = array(l_a)
arr_b = array(l_b)
arr_c = array(l_c)
arr_d = array(l_d)
arr_e = (arr_c - arr_d) - (arr_a - arr_b)**2
print arr_e

(Instead of numarray you can use ScyPy, numerics, etc.)
If your data in on disk you can avoid the list=>array conversion, and
load the data from the numerical library itself, this is probably
almost as fast as doing the same thing in C.

Bye,
bearophile

Jul 4 '06 #10
ma***********@gmail.com wrote:
I hope I am not being too ignorant :p but here goes... my boss has
written a bit of python code and asked me to speed it up for him...
I've reduced the run time from around 20 minutes to 13 (not bad I think
;) to speed it up further I asked him to replace a loop like this:-
index = 0

for element in a:
av = a[index]
bv = b[index]
cv = c[index]
dv = d[index]
avbv = (av-bv) * (av-bv)
diff = cv - dv
e.append(diff - avbv)
index = index + 1
For /real/ speed-ups use a numerical library, e. g.

# untested
from numarray import array
a = array(a)
b = array(b)
c = array(c)
d = array(d)
e = (c-d) - (a-b)*(a-b)

Peter
Jul 4 '06 #11
Peter Otten wrote:
from numarray import array
a = array(a)
b = array(b)
c = array(c)
d = array(d)
e = (c-d) - (a-b)*(a-b)
Oops, bearophile has already posted the same idea with better execution...
Jul 4 '06 #12
be************@lycos.com writes:
[...]
(Instead of numarray you can use ScyPy, numerics, etc.)
If your data in on disk you can avoid the list=>array conversion, and
load the data from the numerical library itself, this is probably
almost as fast as doing the same thing in C.
Apparently if you're starting to write numerical code with Python
these days you should use numpy, not Numeric or numarray.

(Note that in old postings you'll see 'numpy' used as a synonym for
what's now strictly called 'Numeric'. First came Numeric, then the
offshoots/rewrites numarray and scipy-core, and now numpy has come
along to re-unify the two camps -- hooray!)
John
Jul 4 '06 #13

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

Similar topics

9
by: Sebastian Faust | last post by:
Hi, I have a design problem about which I am thinking now for a while and still couldnt find any help in deja. What I need is something like a virtual function template. I know that this is not...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
7
by: Michael | last post by:
I'm writing an application that decodes a file containing binary records. Each record is a particular event type. Each record is translated into ASCII and then written to a file. Each file contains...
1
by: switzerland qunatium computer | last post by:
HERE I BUILT A QUICK MATRIX TOOOK 5 MINS Body: HERE I BUILD ONE FOR YOU 1.http://en.wikipedia.org/wiki/Real-time_computing 2.http://en.wikipedia.org/wiki/Quantum_computing NOW ALL YOU NEED...
6
by: Dasn | last post by:
Hi, there. 'lines' is a large list of strings each of which is seperated by '\t' I wanna split each string into a list. For speed, using map() instead of 'for' loop. 'map(str.split, lines)'...
1
by: =?ISO-8859-15?Q?Ma=EBl_Benjamin_Mettler?= | last post by:
Hello Python-List I hope somebody can help me with this. I spent some time googling for an answer, but due to the nature of the problem lots of unrelevant stuff shows up. Anyway, I...
2
by: WGW | last post by:
Hello all, I need another set of eyes cause it just isn't working, no matter how identical I make it. The initRotator function works when called by itself, but when adding another function, only the...
1
by: Paul Childs | last post by:
Hi folks, I'll start off with the code I wrote... (ActivePython 2.4 on Windows XP SP2) ------------------------------- class FlightCondition(object): lsf = vto =
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.