时钟(),时间()和difftime()在time.h中的问题? [英] questions on clock(),time() and difftime() in time.h?

查看:414
本文介绍了时钟(),时间()和difftime()在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)第一个问题
为什么我的第一主给予我打印时间 288.000000 ,我假定这是288毫秒,但在文件中它应该给我的结果seconds.I上午在Mac上的方式。

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.

2)第二个问题
为什么第二主。
为什么这个我打印输出时间为 0.000000
据我所知,时间()给我一个time_t的结构,我把里面的开始结束 time_t的结构,然后我发现在几秒钟​​的差异,似乎并没有工作。

2)Second Question Why does the second main. Why does this print me the output time being 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.

推荐答案

的返回类型时钟() clock_t表示 - 不一定秒。 <一href=\"https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man3/clock.3.html\"相对=nofollow>苹果:时钟。衡量蜱CPU已经使用的数量( CLOCKS_PER_SEC )。此值不一定毫秒或秒。

The return type from clock() is clock_t - not necessarily seconds. Apple : clock. Which measures the number of ticks the CPU has used (CLOCKS_PER_SEC). This value is not necessarily milliseconds or seconds.

时间()返回因为在某个特定时间点(1/1/1970)的整秒数。

time() returns the number of whole seconds since a particular point in time (1/1/1970).

所以通话时间两次快速连续,在相同数量的整秒的成绩。

So calling time twice in quick succession, results in the same number of whole seconds.

<$ C C> difftime 预计从<$ C C> time_t的(返回类型<$ C C>时间())作为它的参数,并创建一个经过的时间(以秒计)。

difftime expects time_t (return type from time()) as its parameters and creates an elapsed time (in seconds).

因此​​,它是不正确的呼吁 clock_t表示值。

Thus it is not correct to call for clock_t values.

这篇关于时钟(),时间()和difftime()在time.h中的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆