# include # include # include # include # include "closest_point_brute.h" int main ( ); double cpu_time ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* 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 set of NS points in M dimensions. For each S(I) in S, we seek the index J of the point R(J) which is nearest to S(I) over all points in R. Licensing: This code is distributed under the MIT license. Modified: 14 August 2024 Author: John Burkardt Local: int M, the spatial dimension. int NR, the number of data points. int NS, the number of sample points. double R[M*NR], the data points. double S[M*NS], the sample points. */ { int i; int j; int m; double near_dist; int near_index; int nr; double *r; double *s; double t1; double t2; timestamp ( ); printf ( "\n" ); printf ( "closest_point_brute_test():\n" ); printf ( " C version\n" ); printf ( " Test closest_point_brute().\n" ); m = 2; printf ( " Data will have spatial dimension m = %d\n", m ); printf ( "\n" ); nr = 1; while ( nr <= 65536 ) { /* Generate data. */ r = ( double * ) malloc ( m * nr * sizeof ( double ) ); for ( i = 0; i < m; i++ ) { for ( j = 0; j < nr; j++ ) { r[i+j*m] = drand48 ( ); } } /* Generate 1 sample point. */ s = ( double * ) malloc ( m * sizeof ( double ) ); 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 ( ); printf ( " #%5d: %5d %8.2g %g\n", nr, near_index, near_dist, t2 - t1 ); /* Free memory. */ free ( r ); free ( s ); /* Prepare for next loop. */ nr = nr * 2; } /* Terminate. */ printf ( "\n" ); printf ( "closest_point_brute_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ double cpu_time ( ) /******************************************************************************/ /* 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; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* 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: 24 September 2003 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); fprintf ( stdout, "%s\n", time_buffer ); return; # undef TIME_SIZE }