// Rather simple C++ time test. // Builds and sorts array(s) of FLOATING-POINT numbers // using C++ that is more high-level, even if it is a Microsoft compiler. // (Version for Microsoft Visual C++.) #include #include /* Compile options needed:-GX */ #include #include using namespace std; typedef list FLOATLIST; typedef list DWORDLIST; FLOATLIST *make_float_list(int nHowMany) { FLOATLIST *pflist = new FLOATLIST; float i = 20; float j = 245; float k = 8003; while (nHowMany--) { pflist->push_back( (((long unsigned)i) % 20) + (((long unsigned)(j * 4023)) % 145) + (((long unsigned)(k * 5671)) % 879) ); ++i; ++j; ++k; } return pflist; } // FLOATLIST *make_float_list(int nHowMany) // 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 float. 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) ); ++i; ++j; ++k; } return pnMyList; } // float *make_int_array(int) // Stores a time (in milliseconds since Windows started) into array. // Marks position after valid time with 0 value. void add_a_time(DWORDLIST *pdwlistTimes) { pdwlistTimes->push_back(GetTickCount()); } // void add_a_time(DWORDLIST *) // 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(DWORDLIST *pdwlistTimes) { // Temporarily set floating-point precision to be sufficient // to show system time since bootup in milliseconds. int nPrecisionDef = cout.precision(); cout.precision(13); cout << "times : ["; // Iterate through our list of times, printing it to standard output. DWORDLIST::iterator it = pdwlistTimes->begin(); DWORDLIST::iterator end = pdwlistTimes->end(); while (it != end) { cout << (((double)(*it)) / (double)1000); ++it; if (it != end) cout << ", "; } cout << "]\n"; cout.precision(nPrecisionDef); // Undo temporary change to floating-point output precision. } // void print_times(DWORDLIST *) void print_floats(FLOATLIST *pflist) { // Iterate through our list of times, printing it to standard output. FLOATLIST::iterator it = pflist->begin(); FLOATLIST::iterator end = pflist->end(); while (it != end) { cout << *it; ++it; if (it != end) cout << ", "; } cout << "\n"; } // void print_floats(FLOATLIST *pflist) 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.) DWORDLIST dwlistTimes; add_a_time(&dwlistTimes); //printf("RUNNING FUNCTION num_list_test(%d)\n", nHowMany); print_times(&dwlistTimes); //float *pfMyList = make_float_array(nHowMany); FLOATLIST *pflist = make_float_list(nHowMany); add_a_time(&dwlistTimes); //print_floats(pflist); add_a_time(&dwlistTimes); ////qsort((void *)pfMyList, nHowMany, sizeof(int), compare_for_qsort); pflist->sort(); add_a_time(&dwlistTimes); print_floats(pflist); add_a_time(&dwlistTimes); print_times(&dwlistTimes); printf("EXITING FUNCTION num_list_test(%d)\n\n", nHowMany); delete pflist; // Again we can now explicitly see something in our code // 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(30); float_list_test(5); float_list_test(500); float_list_test(5000); float_list_test(50000); float_list_test(100000); return 0; } // main()