0

I've written this code and it does not return the proper time (probably some piece of the code is wrong). And is there any other way to measure time between two actions more precisely then this (in c)?

float measureTime(clock_t start, clock_t end){

    float totalSeconds;    

    totalSeconds = ((long double)(end - start))/CLOCKS_PER_SEC;

    printf("%.6f", totalSeconds);

    return(totalSeconds);
}

int main(){

    clock_t start, end;

    start = clock();
    //piece of code here
    //
    end = clock();

    measureTime(start, end);

    return (EXIT_SUCCESS);
}
Diogo Soares
  • 130
  • 2
  • 8

1 Answers1

0

As Weather Vane said, there are many questions about time measuring in c. Here is one How do I measure time in C?.

Just a comment on your method, you will probably get 0.0s as result if your piece code contains few lines of execution (if you use a modern computer with 4 cpus with a higher frequency, the piece of code will be executed on a few clock cycles).

Community
  • 1
  • 1
shamba
  • 107
  • 7