built in zip function speed

mart.franklin at gmail.com mart.franklin at gmail.com
Tue Jul 4 10:18:29 EDT 2006


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




More information about the Python-list mailing list