Skip to content

Commit 8d0bce6

Browse files
authored
Change dec2bitarray to a faster implementation
The current and previous implementations of dec2bitarray are a huge chunk of time of the simulation. This implementation is the fastest that I have found, comparing with the previously used ones. Is based on this SO answer: https://stackoverflow.com/a/30227161
1 parent 9dfe948 commit 8d0bce6

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

commpy/utilities.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,15 @@ def dec2bitarray(in_number, bit_width):
4646
Array containing the binary representation of all the input decimal(s).
4747
4848
"""
49-
50-
binary_words = vectorized_binary_repr(np.array(in_number, ndmin=1), bit_width)
51-
return np.fromiter(it.chain.from_iterable(binary_words), dtype=np.int8)
49+
result = np.zeros(size, 'int')
50+
i = 1
51+
pox = 0
52+
while i <= number:
53+
if i & number:
54+
result[size - pox - 1] = 1
55+
i <<= 1
56+
pox += 1
57+
return result
5258

5359

5460
def bitarray2dec(in_bitarray):

0 commit comments

Comments
 (0)