开发者

Would you use numpy if you were just manipulating a sequence of binary values?

开发者 https://www.devze.com 2022-12-20 14:44 出处:网络
Is there any advantage to using numpy when you\'re doing a large 开发者_运维技巧number of operations on lists of binary values?How about integers within a small range (like just the numbers 1,2, and 3

Is there any advantage to using numpy when you're doing a large 开发者_运维技巧number of operations on lists of binary values? How about integers within a small range (like just the numbers 1,2, and 3?)


Eliminating the loops is the the source of the performance gain (10x):

import profile
import numpy as NP

def np_test(a2darray) :
  row_sums = NP.sum(a2darray, axis=1)
  return NP.sum(row_sums)

def stdlib_test2(a2dlist) :
  return sum([sum(row) for row in a2dlist])

A = NP.random.randint(1, 6, 1e7).reshape(1e4, 1e3)
B = NP.ndarray.tolist(A)

profile.run("np_test(A)")
profile.run("stdlib_test2(B)")

numpy:

  • 10 function calls in 0.025 CPU seconds

lists:

  • 10005 function calls in 0.280 CPU seconds


If the number of input values is huge, or if you are doing a lot of operations, you might want to try bitarray. Or, see the bool/int8/uint8 dtype in Numpy's ndarray:

In [1]: import numpy as np
In [2]: data = np.array([0,1,1,0], dtype=bool)
In [3]: data
Out[3]: array([False,  True,  True, False], dtype=bool)
In [4]: data.size
Out[4]: 4
In [5]: data.nbytes
Out[5]: 4
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号