why C clock() returns 0

后端 未结 7 1367
自闭症患者
自闭症患者 2020-11-30 12:48

I\'ve got something like this:

clock_t start, end;
start=clock();

something_else();

end=clock();
printf(\"\\nClock cycles are: %d - %d\\n\",start,end);
         


        
7条回答
  •  [愿得一人]
    2020-11-30 13:12

    Well, do you want the time something_else() takes? Try this:

    #include 
    #include   
    #include 
    int main(void) {
        struct timeval start, end;
        long mtime, secs, usecs;    
    
        gettimeofday(&start, NULL);
        something_else();
        gettimeofday(&end, NULL);
        secs  = end.tv_sec  - start.tv_sec;
        usecs = end.tv_usec - start.tv_usec;
        mtime = ((secs) * 1000 + usecs/1000.0) + 0.5;
        printf("Elapsed time: %ld millisecs\n", mtime);
        return 0;
    }
    

提交回复
热议问题