为什么C频率的()返回0 [英] why C clock() returns 0

查看:144
本文介绍了为什么C频率的()返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情:

clock_t start, end;
start=clock();

something_else();

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

和我总是作为输出时钟周期:0 - 0

and I always get as an output "Clock cycles are: 0 - 0"

任何想法,为什么出现这种情况?

Any idea why this happens?

(只给小细节,在something_else()函数执行使用蒙哥马利再presentation左到右幂,而且我不知道肯定,something_else()函数确实需要一些不时间可以忽略不计。)

(Just to give little detail, the something_else() function performs a left-to-right exponentiation using montgomery representation, moreover I don't know for certain that the something_else() function does indeed take some not negligible time.)

这是在Linux上。的uname -a的结果是:

This is on Linux. The result of uname -a is:

Linux的白雪皑皑。*****。​​ac.uk 2.6.32-71.el6.x86_64#1 SMP周五5月20日3点51分51秒BST 2011 x86_64的x86_64的x86_64的GNU / Linux的

推荐答案

时钟函数没有衡量CPU时钟周期。

clock function does not measure CPU clock cycles.

ç说时钟返回的实施的最佳逼近以处理器
因为有关一个实现定义时代的开始使用的节目时间
只对程序调用。

如果两者之间连续时钟叫你计划花费较少的时间比时钟函数的一个统一,你可以GET 0

If between two successive clock calls you program takes less time than one unity of the clock function, you could get 0.

POSIX 时钟定义了 CLOCKS_PER_SEC 1000000(单位为1微秒)。

POSIX clock defines the unity with CLOCKS_PER_SEC as 1000000 (unity is then 1 microsecond).

<一个href=\"http://pubs.opengroup.org/onlinepubs/009604499/functions/clock.html\">http://pubs.opengroup.org/onlinepubs/009604499/functions/clock.html

要测量的x86 / x64的时钟周期就可以使用内联汇编中检索的CPU时间戳计数器寄存器的时钟计数 RDTSC

To measure clock cycles in x86/x64 you can use inline assembly to retreive the clock count of the CPU Time Stamp Counter register rdtsc.

这篇关于为什么C频率的()返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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