gpt4 book ai didi

python - 为什么 numpy 比 python 慢?如何让代码执行得更好

转载 作者:太空狗 更新时间:2023-10-29 21:18:39 24 4
gpt4 key购买 nike

我将我的神经网络从纯 python 改写为 numpy,但现在它的工作速度更慢了。所以我尝试了这两个功能:

def d():
a = [1,2,3,4,5]
b = [10,20,30,40,50]
c = [i*j for i,j in zip(a,b)]
return c

def e():
a = np.array([1,2,3,4,5])
b = np.array([10,20,30,40,50])
c = a*b
return c

时间 d = 1.77135205057

时间 e = 17.2464673758

Numpy 慢了 10 倍。为什么会这样以及如何正确使用 numpy?

最佳答案

我假设差异是因为您在 e 中构建列表和数组,而您仅在 d 中构建列表。考虑:

import numpy as np

def d():
a = [1,2,3,4,5]
b = [10,20,30,40,50]
c = [i*j for i,j in zip(a,b)]
return c

def e():
a = np.array([1,2,3,4,5])
b = np.array([10,20,30,40,50])
c = a*b
return c

#Warning: Functions with mutable default arguments are below.
# This code is only for testing and would be bad practice in production!
def f(a=[1,2,3,4,5],b=[10,20,30,40,50]):
c = [i*j for i,j in zip(a,b)]
return c

def g(a=np.array([1,2,3,4,5]),b=np.array([10,20,30,40,50])):
c = a*b
return c


import timeit
print timeit.timeit('d()','from __main__ import d')
print timeit.timeit('e()','from __main__ import e')
print timeit.timeit('f()','from __main__ import f')
print timeit.timeit('g()','from __main__ import g')

这里函数 fg 避免每次都重新创建列表/数组,我们得到非常相似的性能:

1.53083586693
15.8963699341
1.33564996719
1.69556999207

请注意,list-comp + zip 仍然有效。但是,如果我们使数组足够大,numpy 无疑会胜出:

t1 = [1,2,3,4,5] * 100
t2 = [10,20,30,40,50] * 100
t3 = np.array(t1)
t4 = np.array(t2)
print timeit.timeit('f(t1,t2)','from __main__ import f,t1,t2',number=10000)
print timeit.timeit('g(t3,t4)','from __main__ import g,t3,t4',number=10000)

我的结果是:

0.602419137955
0.0263929367065

关于python - 为什么 numpy 比 python 慢?如何让代码执行得更好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16597066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com