I made a program that unrolls loops and measures the time taken for it to sort an array, but the problem I have is that sometimes the unrolled loop program gives me an answer of 0 in nano seconds and that just can't be. I tried it with MONOTONIC too but just doesn't help. Here's my 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 to store file/Stopwatch.txt", currentTest);
stopwatch = fopen(buf, "a+");
struct stat st = {0};
sprintf(buf, "the place where you stored the stopwatch file", currentTest);
if (stat(buf, &st) == -1)
{
sprintf(buf, "the place where you stored the stopwatch file", currentTest);
mkdir(buf);
}
sprintf(buf, "the place where you store this 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;
}