简体   繁体   English

在time.h中关于clock(),time()和difftime()的问题?

[英]questions on clock(),time() and difftime() in time.h?

int fib(int n,int a,int b){
    if (n==2){
        return a+b;
    }
    return fib(n-1,b,a+b);

}

int main() {
    time_t begin,end;
    begin = clock();
    fib(10000,0,1);
    end = clock();
    printf("%f",difftime(end,begin));
}




int main() {
    time_t begin,end;
    time(&begin);
    fib(10000,0,1);
    time(&end);
    printf("%f",(double)(end-begin)/CLOCKS_PER_SEC);
}

1)First question Why does my first main give prints me a time 288.000000 ,I'm assuming this is 288 milliseconds but in the documentation it is supposed to give me the result in seconds.I am on Mac by the way. 1)第一个问题为什么我的第一个主输出给我打印时间288.000000 ,我假设这是288毫秒,但是在文档中它应该以秒为单位给我结果。顺便说一下,我在Mac上。

2)Second Question Why does the second main. 2)第二个问题为什么要第二个主要。 Why does this print me the output time being 0.000000 . 为什么这样打印输出时间为0.000000 I understand that time() give me a time_t structure that i put inside begin and end time_t structs and then i find the difference in seconds,doesn't seem to work. 我了解到time()给了我一个time_t结构,我将它放在beginend time_t结构中,然后我发现以秒为单位的差异似乎没有用。

Yes i am trying to get the difference in seconds between two difference times. 是的,我正在尝试获取两个不同时间之间的秒数差异。

You could try something like the following approach: 您可以尝试以下方法:

#include <stdio.h>
#include <time.h>


void timeCheck(int *clock,int *minutes){
    int hour ;
    int minute ;
    time_t end, start;
    double diff;
    size_t seconds;


    start = (time_t)((clock[0] * 60 + clock[1]) * 60) ;
    end = (time_t)((minutes[0] * 60 + minutes[1]) * 60) ;

    if( end < start ){
        end += 24 * 60 * 60 ;
    }

    diff = difftime(end, start);

    hour = (int) diff / 3600;
    minute = (int) diff % 3600 / 60;
    printf("The difference is %d:%d\n", hour, minute);
    seconds = (size_t)(hour * 3600) + (size_t)(minute * 60);
    printf("Seconds = %zu\n",seconds);
}

int main(void) {
    int hour[] = {22,20};
    int minutes[] = {5,40};

    printf("Start time %d:%d\n",hour[0],hour[1]);
    printf("End time %d:%d\n\n",minutes[0],minutes[1]);

    timeCheck(hour,minutes);
    return 0;
}

Output: 输出:

 Start time 22:20 End time 5:40 
The difference is 7:20
Seconds = 26400

The return type from clock() is clock_t - not necessarily seconds. clock()的返回类型为clock_t不一定是秒。 Apple : clock . 苹果:时钟 Which measures the number of ticks the CPU has used ( CLOCKS_PER_SEC ). 用来度量CPU使用的滴答数( CLOCKS_PER_SEC )。 This value is not necessarily milliseconds or seconds. 此值不一定是毫秒或秒。

time() returns the number of whole seconds since a particular point in time (1/1/1970). time()返回自特定时间点(1970年1月1日)起的总秒数。

So calling time twice in quick succession, results in the same number of whole seconds. 因此,快速连续两次调用时间将导致相同的整秒数。

difftime expects time_t (return type from time() ) as its parameters and creates an elapsed time (in seconds). difftime time_ttime()返回类型)作为其参数,并创建一个经过的时间(以秒为单位)。

Thus it is not correct to call for clock_t values. 因此,调用clock_t值是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM