I am trying to do bit shifts on numpy
integers (specifically, numpy.uint64
objects) and I need them to be fast. In my implementation below, I put the object in a numpy.array
only because that's the only object that can accept bit left shifts. If there is any faster implementation I will accept it.
from timeit import timeit
print(timeit("a << 1", "a = int(2**60)"))
print(timeit("a << 1", "import numpy as np; a = np.array([2 ** 60], dtype=np.uint64)"))
print(timeit("np.left_shift(a, 1)", "import numpy as np; a = np.array([2 ** 60], dtype=np.uint64)"))
returns:
0.056681648000000084
1.208092987
1.1685176299999998
Why is python so much faster than numpy
for this operation? Is there a way to get comparable speeds in numpy
?