Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way of extracting the last two digits of every element in a numpy array

consider this sample:

sample = np.array([0, 1, 2, 3, 4])

I need the fastest way possible of generating a list/array of the last 2 digits of the binary representation of each value in sample. This way I get the binary representation:

bin_sample = [bin(x) for x in sample]
>>> ['0b0', '0b1', '0b10', '0b11']

I parse each of them like this and get the correct output:

output = [bin(x)[-2:].replace('b','0') for x in sample]
>>> ['00', '01', '10', '11', '00']

The problem is that it is too slow, I'm handling large arrays, any suggestions? Thanks

EDIT: It takes around 5 seconds to process 5 million elements. I need it to take ~ 1 second EDIT #2: Any optimization that gains ~ 500% speed increase is acceptable comparable to the previous algorithm.

like image 211
marcos Avatar asked Mar 02 '23 20:03

marcos


1 Answers

Here is a bit-twiddling solution:

def pp():
    a64 = a.astype(np.int64)
    return (((a64&1)<<32)+((a64&2)>>1)+ord('0')*0x100000001).view('U2')

Lookup done right:

bits_map = np.array(['00', '01', '10', '11'])
def AMC_pp():
    return bits_map[a & 3]
like image 140
Paul Panzer Avatar answered Apr 27 '23 15:04

Paul Panzer