I want to find the points which the distant between points less than 3.For example, some points as follow, (220,221)(220,119)(220,220)(20,90)(220,222). I use (220,221) to find points.Then i can get (220,221)(220,119)(220,220)(220,222) I use (220,119) to find points.Then i can get (220,221)(220,119)(220,220) I have used Nested for loop to do that, but it's very slow.It worked inefficiently.The code as follow,

```
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main()
{
vector<Point> p;
vector<Point> temp;
vector<vector<Point> > centerbox;
p.push_back(Point(110, 110));
p.push_back(Point(110, 111));
p.push_back(Point(110, 110));
p.push_back(Point(110, 112));
p.push_back(Point(111, 112));
p.push_back(Point(150, 111));
for (vector<Point> ::iterator iter1 = p.begin(); iter1 != p.end(); ++iter1) {
for (vector<Point> ::iterator iter2 = p.begin(); iter2 != p.end();) {
if (abs((*iter1).x - (*iter2).x) + abs((*iter1).y - (*iter2).y) < 3) {
temp.push_back((*iter2));
++iter2;
}
else {
++iter2;
}
}
centerbox.push_back(temp);
temp.clear();
}
return 0;
}
```

How can i do to make faster than using Nested for loop?

# Best How To :

for 20000 random points with about 27 neighbors for each point this function gave me a speed-up. It needed about 33% less time than your original method.

```
std::vector<std::vector<cv::Point> > findNeighborsOptimized(std::vector<cv::Point> p, float maxDistance = 3.0f)
{
std::vector<std::vector<cv::Point> > centerbox(p.size()); // already create a output vector for each input point
/*
// if you have enough memory, you can reserve enough space for each point. If not, just remove this part, which will result in a lot of reallocations later.
//unsigned int reserveSize = p.size(); // definitely enough space so no reallocations will happen but probably too much space reserved...
//unsigned int reserveSize = p.size()/2;
//unsigned int reserveSize = p.size()/3;
//unsigned int reserveSize = p.size()/10;
unsigned int reserveSize = 1;
for(unsigned int i=0; i<centerbox.size(); ++i)
{
centerbox[i].reserve(reserveSize);
}
*/
// for me it turned out to be slower to reserve a lot of memory... maybe because of caching?!?
// will depend on the average number of neighbors for each point...
for(unsigned int j=0; j<p.size(); ++j)
{
std::vector<cv::Point> & p1Neighbors = centerbox[j];
// create a reference to the point so that we dont need to GET it several times
cv::Point & p1 = p[j];
//p1Neighbors.push_back(p1); // uncomment this if you want to show the point be a neighbor of itself (dist to itself is alway == 0).
// for p2 ignore p1 and start at the next point
for(unsigned int i=j+1; i<p.size(); ++i)
{
cv::Point & p2 = p[i];
// instead you might want to use cv::norm(p1-p2) or something, but I'll stay at your original logic
if(abs(diff.x) + abs(diff.y) < maxDistance)
{
p1Neighbors.push_back(p2);
// add p1 to neighbors of p2 too
centerbox[j].push_back(p1);
}
}
}
return centerbox;
}
```

depending on the number of points you have in your applications and depending on their distribution, space partitioning or spatial hashing might give you mugh better improvement.