内置拉链功能速度 [英] built in zip function speed

查看:71
本文介绍了内置拉链功能速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我不是太无知:p但是这里......我的老板已经写了一些python代码并要求我为他加速...

我已经将运行时间从大约20分钟减少到13分钟(不错我认为

;)为了加快速度我要求他更换这样的循环: -

index = 0

元素中的元素:

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


(其中a,b,c和d是200,000个元素浮点数组)

使用内置的zip函数..它似乎是为这个问题做的!


for av,bv,cv,dv in zip(a,b,c,d):

avbv =(av-bv)*(av - bv)

diff = cv - dv

e.append(diff - avbv)


然而这似乎运行得多慢于*我*认为它会
(实际上比切片慢)我猜我要问的是..会不会是你希望这个



完整代码清单(我希望我有发了一个非常明显的错误): -


导入数组

导入时间

a = array.array(" f")

b = array.array(" f")

c = array.array(" f")

d = array.array(" ; f")

e = array.array(" f")


表示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


元素中的元素:

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 =时间。时间()


打印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")


##只是为了笑我自己的拉链功能

##笑话是它比内置拉链运行得更快?


def myzip(* args):

index = 0

for elem in args [0]:

zipper = []
arg中arg的


zipper.append(arg [index])

index = index +1

产量拉链


for my,bv,cv,dv myzip(a,b ,c,d):

avbv =(av-bv)*(av - bv)

diff = cv - dv

e。追加(差异 - avbv)


end2 = time.time()


打印end2-end1

$来自400万元素输入阵列的b $ b时间


切片:

8.77999997139

zip():

36.5759999752

myzip( ):

12.1449999809

解决方案

itertools.izip通常比zip更快。你可以试试。




Rune Strand写道:


itertools.izip通常比zip更快。你可以试试。



非常感谢


时间为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)
400万元素aray上的



切片:

8.06299996376

内置拉链:

36.5169999599

myzip:

12.0320000648

izip:

5.76499986649

如此快速整体


Rune Strand写道:


itertools.izip通常比zip更快。你可以试试。



非常感谢


时间为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)
400万元素aray上的



切片:

8.06299996376

内置拉链:

36.5169999599

myzip:

12.0320000648

izip:

5.76499986649

如此快速整体


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

解决方案

itertools.izip is usually faster than zip. You can try that.



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


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


这篇关于内置拉链功能速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆