c - Why is the elapsed time 0? -
i made program unrolls loops , measures time taken sort array, problem have unrolled loop program gives me answer of 0 in nano seconds , can't be. tried monotonic doesn't help. here's code:
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <math.h> #include <unistd.h> #define billion 1000000000l int main() { char buf[500]; int numberofelements = 10; int currenttest = 1; int randomarray[numberofelements]; int minindex; int minvalue; struct timespec requeststart; struct timespec requestend; long int recordstarttime; long int recordendtime; long int elapsedtime; file *arrangedarray; file *stopwatch; sprintf(buf,"place store file/stopwatch.txt", currenttest); stopwatch = fopen(buf, "a+"); struct stat st = {0}; sprintf(buf, "the place stored stopwatch file", currenttest); if (stat(buf, &st) == -1) { sprintf(buf, "the place stored stopwatch file", currenttest); mkdir(buf); } sprintf(buf, "the place store program/arrangedarray.txt", currenttest); arrangedarray = fopen(buf, "w+"); randomarray[0] = 7045; randomarray[1] = 4949; randomarray[2] = 24507; randomarray[3] = 6071; randomarray[4] = 11820; randomarray[5] = 11710; randomarray[6] = 11168; randomarray[7] = 11234; randomarray[8] = 11863; randomarray[9] = 2683; clock_gettime(clock_process_cputime_id, &requeststart); recordstarttime = requeststart.tv_nsec + requeststart.tv_sec * billion; minindex = 0; minindex = 1; minindex = 9; minvalue = randomarray[9]; randomarray[9] = randomarray[0]; randomarray[0] = 2683; minindex = 1; minindex = 2; minindex = 3; minvalue = randomarray[3]; randomarray[3] = randomarray[2]; randomarray[2] = 6071; minindex = 3; minindex = 4; minindex = 5; minindex = 6; minindex = 9; minvalue = randomarray[9]; randomarray[9] = randomarray[3]; randomarray[3] = 7045; minindex = 4; minindex = 5; minindex = 6; minvalue = randomarray[6]; randomarray[6] = randomarray[4]; randomarray[4] = 11168; minindex = 5; minindex = 7; minvalue = randomarray[7]; randomarray[7] = randomarray[5]; randomarray[5] = 11234; minindex = 6; minindex = 7; minvalue = randomarray[7]; randomarray[7] = randomarray[6]; randomarray[6] = 11710; minindex = 7; minindex = 8; minindex = 9; clock_gettime(clock_process_cputime_id, &requestend); recordendtime = (requestend.tv_nsec + requestend.tv_sec * billion); elapsedtime = recordendtime - recordstarttime; sprintf(buf, "%li\n", elapsedtime); fputs(buf, stopwatch); sprintf(buf, "%i\n", randomarray[0]); fputs(buf, arrangedarray); sprintf(buf, "%i\n", randomarray[1]); fputs(buf, arrangedarray); sprintf(buf, "%i\n", randomarray[2]); fputs(buf, arrangedarray); sprintf(buf, "%i\n", randomarray[3]); fputs(buf, arrangedarray); sprintf(buf, "%i\n", randomarray[4]); fputs(buf, arrangedarray); sprintf(buf, "%i\n", randomarray[5]); fputs(buf, arrangedarray); sprintf(buf, "%i\n", randomarray[6]); fputs(buf, arrangedarray); sprintf(buf, "%i\n", randomarray[7]); fputs(buf, arrangedarray); sprintf(buf, "%i\n", randomarray[8]); fputs(buf, arrangedarray); sprintf(buf, "%i\n", randomarray[9]); fputs(buf, arrangedarray); fclose(arrangedarray); fclose(stopwatch); return 0; }
Comments
Post a Comment