为什么Numpy列表访问比Vanilla python慢​​? [英] Why is numpy list access slower than vanilla python?

查看:129
本文介绍了为什么Numpy列表访问比Vanilla python慢​​?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的印象是numpy可以更快地执行列表操作,但是以下示例似乎表明相反:

I was under the impression that numpy would be faster for list operations, but the following example seems to indicate otherwise:

import numpy as np
import time

def ver1():
    a = [i for i in range(40)]
    b = [0 for i in range(40)]
    for i in range(1000000):
        for j in range(40):
            b[j]=a[j]

def ver2():
    a = np.array([i for i in range(40)])
    b = np.array([0 for i in range(40)])
    for i in range(1000000):
        for j in range(40):
            b[j]=a[j]

t0 = time.time()
ver1()
t1 = time.time()
ver2()
t2 = time.time()

print(t1-t0)
print(t2-t1)

输出为:

4.872278928756714
9.120521068572998

(我正在i7 920的Windows 7中运行64位Python 3.4.3)

(I'm running 64-bit Python 3.4.3 in Windows 7, on an i7 920)

我确实知道这不是复制列表的最快方法,但是我试图找出我是否错误地使用了numpy.还是这种情况下numpy的速度较慢,而在更复杂的操作中才更有效率?

I do understand that this isn't the fastest way to copy a list, but I'm trying to find out if I'm using numpy incorrectly. Or is it the case that numpy is slower for this kind of operation and is only more efficient in more complex operations?

我还尝试了以下方法,它只是通过b [:] = a直接复制,而numpy的速度仍然是它的两倍:

I also tried the following, which just just does a direct copy via b[:] = a, and numpy is still twice as slow:

import numpy as np
import time

def ver6():
    a = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    b = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    for i in range(1000000):
        b[:] = a

def ver7():
    a = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
    b = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
    for i in range(1000000):
        b[:] = a

t0 = time.time()
ver6()
t1 = time.time()
ver7()
t2 = time.time()

print(t1-t0)
print(t2-t1)

输出为:

0.36202096939086914
0.6750380992889404

推荐答案

您使用的NumPy错误. NumPy的效率取决于在C级循环中完成尽可能多的工作,而不是解释代码.当你做

You're using NumPy wrong. NumPy's efficiency relies on doing as much work as possible in C-level loops instead of interpreted code. When you do

for j in range(40):
    b[j]=a[j]

这是一个解释循环,具有所有内部解释器的开销以及更多,因为NumPy的索引逻辑比列表索引复杂得多,并且NumPy每次检索元素时都需要创建一个新的元素包装对象.编写这样的代码时,您不会获得NumPy的任何好处.

That's an interpreted loop, with all the intrinsic interpreter overhead and more, because NumPy's indexing logic is way more complex than list indexing, and NumPy needs to create a new element wrapper object on every element retrieval. You're not getting any of the benefits of NumPy when you write code like this.

您需要以这样的方式编写代码,使得工作可以在C中进行:

You need to write the code in such a way that the work happens in C:

b[:] = a

这也将提高列表操作的效率,但是对于NumPy而言,这更为重要.

This would also improve the efficiency of the list operation, but it's much more important for NumPy.

这篇关于为什么Numpy列表访问比Vanilla python慢​​?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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