为什么numpy比python慢​​?如何使代码性能更好 [英] Why is numpy slower than python? How to make code perform better

查看:95
本文介绍了为什么numpy比python慢​​?如何使代码性能更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将神经网络从纯python改为numpy,但现在它的运行速度甚至更慢.所以我尝试了这两个功能:

I revrite my neural net from pure python to numpy, but now it is working even slower. So I tried this two functions:

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

timeit d = 1.77135205057

timeit d = 1.77135205057

timeit e = 17.2464673758

timeit e = 17.2464673758

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

Numpy is 10times slower. Why is it so and how to use numpy properly?

推荐答案

我认为差异是因为您在e中构造列表和数组,而您仅在d中构造列表.考虑:

I would assume that the discrepancy is because you're constructing lists and arrays in e whereas you're only constructing lists in d. Consider:

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避免了每次都重新创建列表/数组,因此我们获得了非常相似的性能:

Here the functions f and g avoid recreating the lists/arrays each time around and we get very similar performance:

1.53083586693
15.8963699341
1.33564996719
1.69556999207

请注意,list-comp + zip仍然获胜.但是,如果我们使数组足够大,则numpy会获得成功:

Note that list-comp + zip still wins. However, if we make the arrays sufficiently big, numpy wins hands down:

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

这篇关于为什么numpy比python慢​​?如何使代码性能更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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