0

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;
    }
  • 1
    Your time might be `0` because no clock tick has ocurred. Please read [this question](http://stackoverflow.com/questions/16740014/computing-time-in-linux-granularity-and-precision) about measuring execution time. – Weather Vane Nov 08 '16 at 13:37
  • I would guess that most of your code will get optimized away because it has no use. (setting the minIndex without using it inbetween for example) – Kami Kaze Nov 08 '16 at 13:40
  • 1
    Additionally to the previous comments: there is very little code between the two calls to `clock_gettime`, so even if it is not optimized away, the elapsed time may be too short to be mesurable. – Jabberwocky Nov 08 '16 at 13:42
  • mmmmmmmh well thanks, I've been trying a full week to get precise measurements on C and I figured that optimization would make the measurements very small but I didn't expect this :D. I did the same experiment on Java. An unrolled sorting algorithm took 18000 nano seconds while the same code unrolled sorting took 3000 nano seconds. Well thanks – Erlandas Aksomaitis Nov 08 '16 at 14:02
  • when compiling, always enable all the warnings Then fix those warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -pedantic` I also use: `-Wconversion -std=gnu99`) The compiler will output a long list of problems with the posted code. Strongly suggest fixing those problems before even thinking about trying to run the code. – user3629249 Nov 10 '16 at 00:17
  • this line: `sprintf(buf,"place to store file/Stopwatch.txt", currentTest);` is characteristic of many of the problems with the posted code. A parameter 'currentTest' is being passed to `sprintf()` but the format string: `place to store file/Stopwatch.txt' does not have '%d' to insert the parameter into the format string. – user3629249 Nov 10 '16 at 00:22
  • when calling (most) system functions: always check the returned value to assure the operation was successful. I.E. `fopen()`, `mkdir()`, `clock_gettime()` – user3629249 Nov 10 '16 at 00:29
  • This is what i got by turning on all waarnings and c99 ||=== Build file: "no target" in "no project" (compiler: unknown) ===| ||warning: command line option '-Wzero-as-null-pointer-constant' is valid for C++/ObjC++ but not for C| ||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===| – Erlandas Aksomaitis Nov 10 '16 at 06:52
  • plus current test was used, but i just simplified the directory so that other people understand how i store stuff, It doesn't matter where I store it so I don't think it's a problem. fopen(), mkdir() all are successful, about clock_gettime() im not so sure... I'll check – Erlandas Aksomaitis Nov 10 '16 at 06:54
  • I turned all options and fixed the wzero problem, there were only three errors and two of them were because two variables were unused.(That was intended) Don't really think that there's a problem there. – Erlandas Aksomaitis Nov 10 '16 at 07:01

0 Answers0