// windows_timing2.c SJ #include #include double dtime(); int main(int argc, char *argv[]) { int N = 10000000; int i; int *A = NULL, *B = NULL; double starttime, endtime, inittime; if (argc > 1) // first command line parameter N = atoi(argv[1]); // allocate, fill, and print input array starttime = dtime(); // take starting time A = (int *) malloc(N * sizeof(int)); for (i = 0; i < N; i++) A[i] = 1; endtime = dtime(); // take ending time inittime = endtime-starttime; // time used if (N < 30) { printf("Original array:"); for (i = 0; i < N; i++) printf(" %d", A[i]); printf("\n"); } else { // for large inputs, print time instead of content printf("Init array of %d elements in %.6lf s, %6.2lf Melem/s\n", N, inittime, (N/inittime)/1000000); } free(A); return 0; } // main() #if defined(_WWWIN32) || defined (WWWIN32) || defined(WIN32) // ms resolution instead of micros -- accurate enough // copied from: https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows #include #include // portable: uint64_t MSVC: __int64 // MSVC defines this in winsock2.h!? /* typedef struct timeval { long tv_sec; long tv_usec; } timeval; */ double dtime() { static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL); SYSTEMTIME system_time; FILETIME file_time; uint64_t time; double ret = 0.0; GetSystemTime( &system_time ); SystemTimeToFileTime( &system_time, &file_time ); time = ((uint64_t)file_time.dwLowDateTime ) ; time += ((uint64_t)file_time.dwHighDateTime) << 32; ret += ((time - EPOCH) / 10000000L); ret += system_time.wMilliseconds / 1000.0; return ret; } #else /** * return current time in seconds (floating point number) */ double dtime() { struct timeval tv; struct timezone tz; gettimeofday(&tv, &tz); return tv.tv_sec + (double)tv.tv_usec/1000000; } #endif