# DTW and kNN-DTW time complexity

I have implemented KNN using a custom DTW metric with sci-kit learn and as shown below:

def dtw(t1, t2):
distance = fastdtw(t1, t2)[0]
return distance
model = KNeighborsClassifier(n_neighbors=1, metric=dtw)


Additionally, I also implemented standalone DTW for template matching as shown below:

 def calc_dtw():
predictions = []
for i in range(len(X)): #X is an array containing X values to be predicted
distances = []
for j in range(len(self.X_)):
distances.append(fastdtw(X[i], self.X_[j])[0]) #self.X_ are the templates
closest = distances.index(min(distances))
predictions.append(self.y_[closest])

return predictions


Both implementations of DTW use the same 'fast dtw' library. I ran a test to calculate how long it takes for each algorithm to compute a prediction and results showed that kNN-DTW is faster than standalone DTW. Shouldn't standalone DTW (quadratic time complexity) be faster than kNN-DTW (cubic time complexity)? Is this because of an error in my DTW implementation?