Numpy"如何填充"方法实施?

时间:2016-07-18 11:13:05

标签: python python-2.7 numpy

您会看到使用Core Python方法并使用Numpy方法创建两个相等的数组:

from time import time
import numpy as np

a = [3] * 100000
b = np.array(a)

问题是Numpy在填充过程中如何比Core Python方法更快:

填充:

st = time()
for i in range(len(a)):
    a[i] = 0
et = time()
print "Core Python need %f seconds" % (et-st)

st = time()
b.fill(0)
et = time()
print "Numpy need %f seconds" %  (et-st) 

结果:

Core Python need 0.014000 seconds
Numpy need 0.000000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.013000 seconds
Numpy need 0.000000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.014000 seconds
Numpy need 0.000000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.013000 seconds
Numpy need 0.001000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.013000 seconds
Numpy need 0.001000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.013000 seconds
Numpy need 0.000000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.013000 seconds
Numpy need 0.000000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.014000 seconds
Numpy need 0.000000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.013000 seconds
Numpy need 0.000000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.013000 seconds
Numpy need 0.000000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.013000 seconds
Numpy need 0.000000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.014000 seconds
Numpy need 0.000000 seconds
>>> ================================ RESTART ================================
>>> 
Core Python need 0.014000 seconds
Numpy need 0.000000 seconds
>>> 

2 个答案:

答案 0 :(得分:0)

如果包含将a转换为numpy数组的时间,那么核心python方法需要大约相同的时间。

In [14]: %%timeit
   ....: a = [3] * 100000
   ....: for i in range(len(a)):
   ....:     a[i] = 0
   ....:
100 loops, best of 3: 9.94 ms per loop

In [15]: %%timeit
   ....: a = [3] * 100000
   ....: b = np.array(a)
   ....: b.fill(0)
   ....:
100 loops, best of 3: 9.47 ms per loop

答案 1 :(得分:0)

你的2'阵列'可能具有相同的值,但它们不会以相同的方式存储

a = [3] * 100000
b = np.array(a)

a作为一个列表,包含一个指向数字3的100000个指针(存储在内存中的其他位置)。 a[i]=0更改其中一个指向0位置的指针。你的循环以解释的Python速度更改每个指针。

很多时间都花在了循环机制上:

In [139]: %%timeit a=[3]*100000
     ...: for i in range(len(a)):a[i]=0    
100 loops, best of 3: 13.4 ms per loop

In [140]: %%timeit a=[3]*100000
     ...: for i in range(len(a)):pass 
100 loops, best of 3: 5.86 ms per loop

b是一个新结构,一个numpy数组。从列表中创建它需要一些时间:

In [142]: %timeit b=np.array(a)
100 loops, best of 3: 15.8 ms per loop

但是一旦创建,numpy就可以快速,编译的代码速度执行许多任务。 b.fill就是其中之一。

In [144]: %%timeit b=np.array(a)
     ...: b.fill(0)
10000 loops, best of 3: 74 µs per loop

由于代码已编译,您无法轻易了解fill是如何完成的。

b是一个具有形状和步幅等属性的对象,以及一个数据缓冲区。在这种情况下,缓冲区以包含许多c的{​​{1}}数组开始(不是指向3的指针)。 3 numpy执行快速fill(0)操作,将c写入每个字节。

所以有2个关键差异

  • 存储(点列表v值列表)
  • 解释v编译代码。