I have the following code:
def func(value, start=None, end=None):
if start is not None and start != 0:
start = -start
elif start == 0:
start = None
if end is not None:
end = -end - 1
return int('{:032b}'.format(value)[end:start], 2)
data = np.random.randint(1, 429496729, 10000)
starts = [10, 50, 100, 200]
stops = [30, 90, 170, 250]
data_dict = [{} for _ in range(len(starts))]
for ii, (start, stop) in enumerate(zip(starts, stops)):
range_array = np.arange(start, stop, 2)
data_dict[ii]['one'] = [func(value, 0, 8) for value in data[range_array]]
data_dict[ii]['two'] = [func(value, 9, 17) for value in data[range_array]]
data_dict[ii]['three'] = [func(value, 27, 27) for value in data[range_array]]
data_dict[ii]['four'] = [func(value, 28, 28) for value in data[range_array]]
The problem is that this code runs through relatively slowly. However, all other approaches I have tried so far are even slower. Does anyone have an idea how to rewrite this code so that it runs through faster?