I am new to python. Was trying to solve a problem but stuck due to TLE. The following code is taking too much time, around 10 sec. Now, I'm wondering if normal nested looping is so much inefficient or I am doing something wrong?
from datetime import datetime
arr = [1 for i in range(10000)]; # Originally had large size array of length ~10^4
l = len(arr);
ans = 0;
time1 = datetime.now();
# arr = sorted(arr);
for i in range(l):
for j in range(i+1,l):
ans+= arr[i]*arr[j];
print(datetime.now() - time1);
The output to the above code:
0:00:10.595463
I already know that python is based on interpreter and is slow than compiled languages like C++ or Java. But this is a lot!
Since python indexing is done in O(1), this shouldn't take so much time.
Please help me understand if this is normal behaviour of python or anything needs to be changed here.
Although I can use numpy but want to use this in native fashion. Please help.