Для меня KDTree намного быстрее:
>>> import numpy as np
>>> from scipy.spatial import cKDTree as KDTree
>>> from timeit import timeit
>>>
>>> z = np.random.randn(1000, 2)
>>> k = 5
>>>
>>> KDTree(z).query(z, k)
(array([[0. , 0.21130505, 0.22903208, 0.32009477, 0.38000444],
[0. , 0.03969915, 0.06698214, 0.08423566, 0.10740011],
[0. , 0.04964421, 0.08194808, 0.11576068, 0.12022531],
...,
[0. , 0.00721785, 0.03346301, 0.03617199, 0.04193239],
[0. , 0.05147871, 0.05619545, 0.08028866, 0.08744349],
[0. , 0.03733766, 0.06359033, 0.06861222, 0.0698981 ]]), array([[ 0, 391, 134, 462, 575],
[ 1, 87, 879, 846, 122],
[ 2, 793, 314, 564, 483],
...,
[997, 390, 432, 165, 952],
[998, 194, 457, 775, 629],
[999, 158, 522, 862, 791]]))
>>> nearst_sort(*z.T, k)
array([[ 0., 391., 134., 462., 575.],
[ 1., 87., 879., 846., 122.],
[ 2., 793., 314., 564., 483.],
...,
[997., 390., 432., 165., 952.],
[998., 194., 457., 775., 629.],
[999., 158., 522., 862., 791.]])
>>> timeit(lambda: KDTree(z).query(z, k), number=100)
0.12790076900273561
>>> timeit(lambda: nearst_sort(*z.T, k), number=100)
6.5285790269990684
Это 50-кратный коэффициент. Может зависеть от примера.