-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdtree2_test.cpp
141 lines (109 loc) · 3.45 KB
/
kdtree2_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// A demonstration of using the KDTREE2 C++ routines, and timing.
// This file is in the public domain.
//
#include "kdtree2.hpp"
#include <boost/multi_array.hpp>
#include <boost/random.hpp>
static boost::minstd_rand generator(42u);
static boost::uniform_real<> uni_dist(0,1);
boost::variate_generator<boost::minstd_rand&,boost::uniform_real<> > uni(generator,uni_dist);
float random_variate() {
// between [0,1)
return(uni());
}
//
// define, for convenience a 2d array of floats.
//
typedef boost::multi_array<float,2> array2dfloat;
#include <ctime>
float time_a_search(kdtree2::KDTree* tree, int nn, int nsearch) {
int dim = tree->dim;
std::vector<float> query(dim);
kdtree2::KDTreeResultVector result;
clock_t t0, t1;
t0 = clock();
for (int i=0; i<nsearch;i++) {
for (int j=0; j<dim; j++) query[j] = random_variate();
tree->n_nearest(query,nn,result);
}
t1 = clock();
return(static_cast<float>
(static_cast<double> (t1-t0) / static_cast<double> (CLOCKS_PER_SEC) ));
}
void time_random_searches(kdtree2::KDTree* tree, int nn) {
// emit the number of searches per second.
int nsearch;
nsearch = 50;
for(;;) {
float t = time_a_search(tree,nn,nsearch);
if (t < 1.0) {
nsearch *= 5;
continue;
} else {
float sps = float(nsearch) / t ;
std::cout << "C++ impl, for nn=" << nn << " searches/sec = " << sps << "\n";
return;
}
}
}
int main() {
array2dfloat data(boost::extents[10][3]); // declare a 10000 x 3 array.
array2dfloat realdata;
// notice it is in C-standard layout.
kdtree2::KDTree* tree;
kdtree2::KDTreeResultVector res;
int N, dim;
if (false) {
for (int i=0; i<10; i++) {
for (int j=0; j<3; j++)
data[i][j] = static_cast<float> (3*i+j);
}
tree = new kdtree2::KDTree(data,true);
// tree->dump_data();
//data[0][0]=666.0; // mutate it underneath. DO NOT DO THIS IN REAL USE
//tree->dump_data(); // test to see it change there.
tree->n_nearest_around_point(1,1,1,res);
for (unsigned int i=0; i<res.size(); i++) {
printf("result[%d]= (%d,%f)\n",i,res[i].idx,res[i].dis);
}
delete tree;
}
printf("Give me N, and dim (e.g. '1000 3'). No commas!");
scanf("%d %d",&N,&dim);
printf("I found N=%d,dim=%d\n",N,dim);
realdata.resize(boost::extents[N][dim]);
for (int i=0; i<N; i++) {
for (int j=0; j<dim; j++)
realdata[i][j] = random_variate();
}
tree = new kdtree2::KDTree(realdata,true);
tree->sort_results = true;
std::cout << "Tree created, now testing against brute force...";
{
std::vector<float> query(dim);
kdtree2::KDTreeResultVector result, resultbrute;
int nn = 10;
for (int i=0; i<50; i++) {
for (int j=0; j<dim; j++) query[j] = random_variate();
tree->n_nearest_brute_force(query,nn,resultbrute);
tree->n_nearest(query,nn,result); // search for 10 of them.
for (int k=0; k<nn; k++) {
if ((resultbrute[k].dis != result[k].dis) ||
(resultbrute[k].idx != result[k].idx)) {
std::cout << "Mismatch! nn=" << k << " brute=[" <<
resultbrute[k].dis << "," << resultbrute[k].idx <<
"] tree=[" << result[k].dis << "," << result[k].idx << "]\n";
}
}
}
}
std::cout << "\nTesting complete. Now testing timing...\n";
tree->sort_results = false;
{
int nnarray[] = {1,5,10,25,500} ;
for (int i=0; i< 5; i++) {
time_random_searches(tree,nnarray[i]);
}
}
}