# include # include # include # include "closest_pair_brute.h" int main ( ); double closest_pair_brute_test01 ( int n ); double cpu_time ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: closest_pair_brute_test() tests closest_pair_brute(). Licensing: This code is distributed under the MIT license. Modified: 03 April 2024 Author: John Burkardt */ { int i; int nvec[8] = { 100, 200, 400, 800, 1600, 3200, 6400, 12800 }; double svec[8]; timestamp ( ); printf ( "\n" ); printf ( "closest_pair_brute_test()\n" ); printf ( " C version\n" ); printf ( " Test closest_pair_brute().\n" ); /* Solve a sequence of problems of increasing size N. */ for ( i = 0; i < 8; i++ ) { svec[i] = closest_pair_brute_test01 ( nvec[i] ); } /* Print the final table. */ printf ( "\n" ); printf ( " Test Points Time\n" ); printf ( "\n" ); for ( i = 0; i < 8; i++ ) { printf ( " %4d %6d %14.6g\n", i, nvec[i], svec[i] ); } /* Terminate. */ printf ( "\n" ); printf ( "closest_pair_brute_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ double closest_pair_brute_test01 ( int n ) /******************************************************************************/ /* Purpose: closest_pair_brute_test01() tests the closest_pair_brute program. Licensing: This code is distributed under the MIT license. Modified: 03 April 2024 Author: John Burkardt Input: int n: the number of points. Output: double s: execution time in seconds. */ { double d; int i; int j; double s; long int seed; double *xy; printf ( "\n" ); printf ( "closest_pair_brute_test01():\n" ); printf ( " closest_pair_brute() on a set of N points in 2D.\n" ); xy = ( double * ) malloc ( n * 2 * sizeof ( double ) ); seed = 123456789L; srand48 ( seed ); for ( j = 0; i <= 1; j++ ) { for ( i = 0; i <= n; i++ ) { xy[i+j*n] = drand48 ( ); } } s = cpu_time ( ); closest_pair_brute ( n, xy, &d, &i, &j ); s = cpu_time ( ) - s; printf ( "\n" ); printf ( " n = %d\n", n ); printf ( " time in seconds = %g\n", s ); printf ( " xy(%d) = (%g,%g)\n", i, xy[i+0*n], xy[i+1*n] ); printf ( " xy(%d) = (%g,%g)\n", j, xy[j+0*n], xy[j+1*n] ); printf ( " distance = %g\n", d ); free ( xy ); return s; } /******************************************************************************/ 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: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 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 ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }