// Rather simple C++ time test. // Builds and sorts array(s) of FLOATING-POINT numbers // using C++ that is not terribly general // and is rather low-level, really rather C-ish. // (Version for Microsoft Visual C++.) #include #include // Builds a "list" (actually a dynamically allocated array) of floats. float *make_float_array(int nHowMany) { float *pnMyList = (float *) malloc(sizeof(float) * nHowMany); if (pnMyList == NULL) { printf("Failed to allocate memory for floats list.\n"); } float *pfWhere = pnMyList; // Where we are storing int. float i = 20; float j = 245; float k = 8003; while (nHowMany--) { *(pfWhere++) = ( (((long unsigned)i) % 20) + (((long unsigned)(j * 4023)) % 145) + (((long unsigned)(k * 5671)) % 879) ); // Note: Visual C++ is not permitting us to use % with float arguments. ++i; ++j; ++k; } return pnMyList; } // float *make_float_array(int) // Stores a time (in milliseconds since Windows started) into array. // Marks position after valid time with 0 value. void add_a_time(DWORD *pdwTimes) { while (*pdwTimes) ++pdwTimes; *pdwTimes = GetTickCount(); *(pdwTimes + 1) = 0; // Marks end of valid times. } // void add_a_time(DWORD adwTimes[]) // Prints valid times in times array (values from GetTickCount(), terminated with 0.) void print_times(DWORD *pdwTimes) { while (*pdwTimes) { printf("%f secs, ", ((float) *pdwTimes) / 1000); ++pdwTimes; } printf("(end)\n"); } // void print_times(DWORD adwTimes[]) void print_floats(float *pf, int nHowMany) { while (nHowMany) { printf("%f", *(pf++)); if (--nHowMany) printf(", "); } printf("\n"); } // void print_floats(float *pn, int nHowMany) int compare_for_qsort( const void *arg1, const void *arg2 ) { float f1 = *( (float *) arg1); // Circumlocutions like this are one ... float f2 = *( (float *) arg2); // "benefit" of a strongly typed language? if (f1 < f2) return -1; if (f1 == f2) return 0; return 1; } // int compare_for_qsort( const void *arg1, const void *arg2 ) void float_list_test(int nHowMany) { DWORD adwTimes[16] = {0}; // Times when we checked the system time // (in milliseconds, zero-terminated.) add_a_time(adwTimes); printf("RUNNING FUNCTION num_list_test(%d)\n", nHowMany); print_times(adwTimes); float *pfMyList = make_float_array(nHowMany); add_a_time(adwTimes); qsort((void *)pfMyList, nHowMany, sizeof(int), compare_for_qsort); add_a_time(adwTimes); print_floats(pfMyList, nHowMany); add_a_time(adwTimes); print_times(adwTimes); printf("EXITING FUNCTION num_list_test(%d)\n\n", nHowMany); free((void *) pfMyList); // Look, we can now explicitly see something // that we aren't timing in *any* of our versions! } // void float_list_test(int nHowMany) int main() { printf("hello world.\n"); //float_list_test(20); float_list_test(5); float_list_test(5000); float_list_test(50000); float_list_test(100000); return 0; } // main()