I need to have huge boolean array. All values should be initialized as "True":
arr = [True] * (10 ** 9)
But created as above it takes too much memory. So I decided to use bytearray
for that:
arr = bytearray(10 ** 9) # initialized with zeroes
Is it possible to initialize bytearray
with b'\x01'
as effectively as it is initialized by b'\x00'
?
I understand I could initialize bytearray
with zeros and inverse my logic. But I'd prefer not to do that if possible.
timeit:
>>> from timeit import timeit
>>> def f1():
... return bytearray(10**9)
...
>>> def f2():
... return bytearray(b'\x01'*(10**9))
...
>>> timeit(f1, number=100)
14.117428014000325
>>> timeit(f2, number=100)
51.42543800899875