[英]Python: Fastest way of packing a 2d array of binary values into UINT64 array
我有一個大小為(149797, 64)的 2D UINT8 numpy 數組。 每個元素都是 0 或 1。我想將每一行中的這些二進制值打包成一個UINT64值,以便我得到一個形狀為 149797 的 UINT64 數組。 我使用 numpy bitpack 函數嘗試了以下代碼。
test = np.random.randint(0, 2, (149797, 64),dtype=np.uint8)
col_pack=np.packbits(test.reshape(-1, 8, 8)[:, ::-1]).view(np.uint64)
packbits 函數的執行時間大約為10 毫秒。 對該數組本身的簡單整形似乎需要大約7 毫秒。我還嘗試使用移位操作迭代 2d numpy 數組以達到相同的結果; 但速度沒有提高。
最后,我還想使用numba for CPU 編譯它。
@njit
def shifting(bitlist):
x=np.zeros(149797,dtype=np.uint64) #54
rows,cols=bitlist.shape
for i in range(0,rows): #56
out=0
for bit in range(0,cols):
out = (out << 1) | bitlist[i][bit] # If i comment out bitlist, time=190 microsec
x[i]=np.uint64(out) # Reduces time to microseconds if line is commented in njit
return x
使用njit大約需要6 毫秒。
這是並行的 njit 版本
@njit(parallel=True)
def shifting(bitlist):
rows,cols=149797,64
out=0
z=np.zeros(rows,dtype=np.uint64)
for i in prange(rows):
for bit in range(cols):
z[i] = (z[i] * 2) + bitlist[i,bit] # Time becomes 100 micro if i use 'out' instead of 'z[i] array'
return z
它的執行時間為3.24 毫秒(google colab 雙核 2.2Ghz)略好一些,目前,使用swapbytes(Paul's)方法的 python 解決方案似乎是最好的,即1.74 ms 。
我們怎樣才能進一步加速這種轉化? 是否有使用任何矢量化(或並行化)、位數組等來實現加速的余地?
參考: numpy packbits 打包到 uint16 數組
在12 核機器上(Intel(R) Xeon(R) CPU E5-1650 v2 @ 3.50GHz),
Pauls 方法: 1595.0微秒(我想它不使用多核)
Numba 代碼: 146.0微秒(上述並行 numba)
即大約 10 倍加速!!!
通過使用byteswap
而不是重塑等,您可以獲得相當大的加速:
test = np.random.randint(0, 2, (149797, 64),dtype=np.uint8)
np.packbits(test.reshape(-1, 8, 8)[:, ::-1]).view(np.uint64)
# array([ 1079982015491401631, 246233595099746297, 16216705265283876830,
# ..., 1943876987915462704, 14189483758685514703,
12753669247696755125], dtype=uint64)
np.packbits(test).view(np.uint64).byteswap()
# array([ 1079982015491401631, 246233595099746297, 16216705265283876830,
# ..., 1943876987915462704, 14189483758685514703,
12753669247696755125], dtype=uint64)
timeit(lambda:np.packbits(test.reshape(-1, 8, 8)[:, ::-1]).view(np.uint64),number=100)
# 1.1054180909413844
timeit(lambda:np.packbits(test).view(np.uint64).byteswap(),number=100)
# 0.18370431219227612
有點 Numba 解決方案(版本 0.46/Windows)。
代碼
import numpy as np
import numba as nb
#with memory allocation
@nb.njit(parallel=True)
def shifting(bitlist):
assert bitlist.shape[1]==64
x=np.empty(bitlist.shape[0],dtype=np.uint64)
for i in nb.prange(bitlist.shape[0]):
out=np.uint64(0)
for bit in range(bitlist.shape[1]):
out = (out << 1) | bitlist[i,bit]
x[i]=out
return x
#without memory allocation
@nb.njit(parallel=True)
def shifting_2(bitlist,x):
assert bitlist.shape[1]==64
for i in nb.prange(bitlist.shape[0]):
out=np.uint64(0)
for bit in range(bitlist.shape[1]):
out = (out << 1) | bitlist[i,bit]
x[i]=out
return x
時間安排
test = np.random.randint(0, 2, (149797, 64),dtype=np.uint8)
#If you call this function multiple times, only allocating memory
#once may be enough
x=np.empty(test.shape[0],dtype=np.uint64)
#Warmup first call takes significantly longer
res=shifting(test)
res=shifting_2(test,x)
%timeit res=shifting(test)
#976 µs ± 41.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit res=shifting_2(test,x)
#764 µs ± 63 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit np.packbits(test).view(np.uint64).byteswap()
#8.07 ms ± 52.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit np.packbits(test.reshape(-1, 8, 8)[:, ::-1]).view(np.uint64)
#17.9 ms ± 91 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.