0

I need to know how many times i spend to run a particular method in C but the timer does not work and it prints the value: 0.000000. I read that the right code is:

clock_t a,b;
a=clock();
//code...
b=clock();
double result =(double) (a-b)/CLOCKS_PER_SEC;

But it doesn't work with my program.

int calculate (char *file1, char *file2,int param,char* path ){
double timeSpent = 0.0;
clock_t begin,finish;
begin = clock();
int dim1=strlen(file1);
int dim2= strlen(file2);
int i;
int **ppm =matrix(file1,dim1,file2,dim2,createMatrix(dim1,dim2));
int distance = ppm[dim1][dim2];
struct Instruction istr[distance + 1];
int tmp = start(ppm, file1, &dim1, file2, &dim2, istr);
end(file1, &dim1, file2, &dim2, istr, tmp);
for (i = 0; i <= dim1; i++)
    free(ppm[i]);
free(ppm);
if (numDiv > 0){
    numDiv--;}
finish= clock();
timeSpent=(double ) (finish-begin)/CLOCKS_PER_SEC;
printf("time: %3f",timeSpent);
}
  • 1
    Welcome to Stack Overflow! Please learn [how to ask](https://stackoverflow.com/help/how-to-ask), [how not to ask](https://stackoverflow.com/help/dont-ask) and [what's on-topic](https://stackoverflow.com/help/on-topic) before posting a question. We need to understand the problem correctly *and* entirely so please add all the necessary details that clarify your issue and/or (when related to code) post a [minimal complete reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MCRE) with the actual and expected behavior as well as (if possible) the actual input. – RobertS supports Monica Cellio Aug 25 '20 at 13:58
  • 1
    We also expect from you [to do at least a little research](https://meta.stackoverflow.com/q/261592) and/or (when related to code) [writing code to solve the issue](https://meta.stackoverflow.com/q/334822) *on your own*. Stack Overflow is not a code request nor a tutorial service for explaining fundamental code elements or techniques in general. Questions with insufficient information to understand or reproduce the issue or do not show any effort to resolve the problem tend to get closed very soon. – RobertS supports Monica Cellio Aug 25 '20 at 13:58
  • If you use any function you don't know already, you must *immediately* open the documentation for it. Depending on your platform, that'd either be a `man`-page on unix (Linux or Mac), or a Microsoft documentation page on Windows. For Windows, https://learn.microsoft.com/en-us/search/?terms=clock would give you the answer in the top-most result. For Linux, either do `man clock` from terminal, or look it up online: https://www.google.com/search?q=clock&sitesearch=man7.org%2Flinux%2Fman-pages&sa=Search+online+pages. On the Mac, ⌘-Space terminal Enter, then `man clock` from the terminal. – Kuba hasn't forgotten Monica Aug 25 '20 at 16:14

0 Answers0