I have a task:
How many pairs of (i,j): array_1[ i ] + array_1[ j ] > array_2[ i ] + array_2[ j ]
This is my code:
import numpy as np
import pandas as pd
n = 200000
series_1 = np.random.randint(low = 1,high = 1000,size = n)
series_1_T = series_1.reshape(n,1)
series_2 = np.random.randint(low = 1,high = 1000,size = n)
series_2_T = series_2.reshape(n,1)
def differ(x):
count = 0
tabel_1 = series_1 + series_1_T[x:x+2000]
tabel_2 = series_2 + series_2_T[x:x+2000]
diff= tabel_1[tabel_1>tabel_2].shape[0]
count += diff
return count
arr = pd.DataFrame(data = np.arange(0,n,2000),columns = ["numbers"])
count_each_run = arr["numbers"].apply(differ) #this one take about 8min 40s
print(count_each_run.sum())
Are there any ways to speedup this?