Search code examples
cclock

Timing program with clock()


I'm trying to time my program using time.h. Code looks like below and I'm using gcc to compile.

My problem is when I try to run this code (I'm using Visual Studio Code), start and end have approximately the same time, and solutionFound is outrageously large for some reason (several orders of magnitude larger than end, occasionally negative). Obviously these times are not right, but I don't know why or how to fix it.

main.c

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main()
{
    // some input/output using printf() and scanf() to get user options
    int maxLength; scanf("%d", &maxLength);
    
    clock_t     start,              solutionFound, end;
    long double timeToFindSolution, timeToRun;

    start = clock();
    for (int i = 1; i < maxLength; i++;)
    {
        // work...
        if(match()) solutionFound = clock(); // match() defined elsewhere in file
    }

    end = clock();

    timeToFindSolution = ((long double) (solutionFound - start)) / CLOCKS_PER_SEC;
    timeToRun          = ((long double) (end - start))           / CLOCKS_PER_SEC;

    printf("solution found in %Lf, took %Lf to run\n", timeToFindSolution, timeToRun);
    printf("start = %f, foundPswd = %f, end = %f\n", (double)start, (double)solutionFound, (double)end);

    return 0;
}

I tried putting end = clock() inside my loop but that had no effect. I found a lot of questions on Stack Overflow about how to time a program but didn't see anyone that had a similar problem.

I also tried making timeToFindSolution and timeToRun doubles instead of long doubles (not expecting the program to run very long anyway), still had the same problem.


Solution

  • Well, if we consider this part of your code:

    for (int i = 1; i < maxLength; i++;)
    {
        // work...
        if(match()) solutionFound = clock(); // match() defined elsewhere in file
    }
    

    you're not always guaranteed that you'll find a match, so the variable solutionFound will contain garbage data that was not intended to be there (in case no match was found).

    The main reason causing this issue is that the stack-frame of the function main or any function, in general, does not clean up beforehand. Variables declared (but not initialized) inside a function belongs to a section called .bss reserved for uninitialized data. On the runtime of the executable these variables don't know what values should contain, so inherently they take the existing content on stack, which explains the fluctuating and sometimes negative values, as every time you run your program it will occupy a region of the memory that was occupied by another program that left it's content behind, and you're simply using that content (unwillingly). So remember to initialize your variables when you're not sure it they will be set somewhere.

    I suggest you initialize it to zero and update the way you handle the case when you don't have a match.