# include # include # include # include # include using namespace std; # include "closest_point_brute.hpp" int main ( ); double cpu_time ( ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // closest_point_brute_test() tests closest_point_brute(). // // Discussion: // // We are given R, a set of NR points in M dimensions. // // We are given S, a point in M dimensions. // // We seek the index J of the point R(J) // which is nearest to S over all points in R. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 15 August 2024 // // Author: // // John Burkardt // // Local: // // int M, the spatial dimension. // // int NR, the number of data points. // // double R[M*NR], the data points. // // double S[M], the sample point. // { int i; int j; int m; double near_dist; int near_index; int nr; double *r; double *s; double t1; double t2; timestamp ( ); cout << "\n"; cout << "closest_point_brute_test():\n"; cout << " C++ version\n"; cout << " Test closest_point_brute().\n"; m = 2; cout << " Data will have spatial dimension m = " << m << "\n"; cout << "\n"; nr = 1; while ( nr <= 65536 ) { // // Generate data. // r = new double[m*nr]; for ( i = 0; i < m; i++ ) { for ( j = 0; j < nr; j++ ) { r[i+j*m] = drand48 ( ); } } // // Generate 1 sample point. // s = new double[m]; for ( i = 0; i < m; i++ ) { s[i] = drand48 ( ); } // // Find closest point in R to S. // t1 = cpu_time ( ); closest_point_brute ( m, nr, r, s, near_index, near_dist ); t2 = cpu_time ( ); cout << " #" << setw(5) << nr << " " << setw(5) << near_index << " " << setw(8) << near_dist << " " << t2 - t1 << "\n"; // // Free memory. // delete [] r; delete [] s; // // Prepare for next loop. // nr = nr * 2; } // // Terminate. // cout << "\n"; cout << "closest_point_brute_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 double cpu_time ( ) //****************************************************************************80 // // Purpose: // // cpu_time() returns the current reading on the CPU clock. // // Discussion: // // The CPU time measurements available through this routine are often // not very accurate. In some cases, the accuracy is no better than // a hundredth of a second. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 06 June 2005 // // Author: // // John Burkardt // // Output: // // double CPU_TIME, the current reading of the CPU clock, in seconds. // { double value; value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC; return value; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // Purpose: // // timestamp() prints the current YMDHMS date as a time stamp. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 March 2018 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }