I have an array looking like this:
testdata = [-2, -1, 0, 1, 2, 3, 10, 3, 2, 1, 0, -1, -2]
So it has a maximum value and to the left and right the values gradually go down to zero and it then can have values below 0.
The aim of my code is to find the maximum value of the array and sum all these values left and right to it until the value 0 (including the maximum value).
To test my code I generated a larger array like this (ignoring possible smaller values than 0):
data1 = [x for x in range(0, 100000, 1)]
data2 = [x for x in range(100000, -1, -1)]
data3 = data1 + data2
The fastest code I can come up with looks like this:
j = 1
k = 0
max_index = np.where(data3 == np.amax(data3))[0][0]
while data3[max_index + j] > 0:
j += 1
while data3[max_index - k] > 0:
k += 1
summ1 = np.sum(data3[max_index:(max_index+j)])
summ2 = np.sum(data3[(max_index-k):max_index])
total = summ1 + summ2
print(total)
Any suggestions on how to boost this even faster?