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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With