编程语言
首页 > 编程语言> > 首页>Python>如果您只是在操纵二进制值序列,您会使用numpy吗?

首页>Python>如果您只是在操纵二进制值序列,您会使用numpy吗?

作者:互联网

当您对二进制值列表进行大量操作时,使用numpy有什么好处吗?小范围内的整数(例如数字1,2和3)如何?

解决方法:

消除循环是提高性能(10倍)的源泉:

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的:

在0.025 CPU中> 10个函数调用

清单:

> 0.280 CPU中的10005个函数调用

标签:scipy,bit-manipulation,python,numpy
来源: https://codeday.me/bug/20191106/2001224.html