时钟()在time.h中precision [英] clock() precision in time.h
问题描述
我试图来计算数量蜱函数用来运行和这样做的使用时钟()
功能,像这样:
I am trying to calculate the number of ticks a function uses to run and to do so an using the clock()
function like so:
unsigned long time = clock();
myfunction();
unsigned long time2 = clock() - time;
printf("time elapsed : %lu",time2);
但问题是它返回的值是10000多,我认为这是在 CLOCK_PER_SECOND
。有没有一种方法,或者更precise?
But the problem is that the value it returns is a multiple of 10000, which I think is the CLOCK_PER_SECOND
. Is there a way or an equivalent function value that is more precise?
我使用Ubuntu 64位,但将preFER如果解决方案可以在其他系统,如Windows和放大器工作;的Mac OS。
I am using Ubuntu 64-bit, but would prefer if the solution can work on other systems like Windows & Mac OS.
推荐答案
有一些POSIX中更精确的定时器。
There are a number of more accurate timers in POSIX.
- <$c$c>gettimeofday()$c$c> - 正式过时,但很广泛使用;微秒分辨率。
- <$c$c>clock_gettime()$c$c> - 替换
函数gettimeofday()
(但并不一定如此广泛的使用;在Solaris上,需要-lposix4
链接) ,具有纳秒分辨率。
gettimeofday()
- officially obsolescent, but very widely available; microsecond resolution.clock_gettime()
- the replacement forgettimeofday()
(but not necessarily so widely available; on Solaris, requires-lposix4
to link), with nanosecond resolution.
有或大或小的古代,可移植性和决议的其他亚秒计时器,包括:
There are other sub-second timers of greater or lesser antiquity, portability, and resolution, including:
-
FTIME()
- 毫秒的分辨率 -
时钟()
- 你已经知道了 -
倍()
-CLK_TCK
或HZ
ftime()
- millisecond resolutionclock()
- which you already know abouttimes()
-CLK_TCK
orHZ
不要使用 FTIME()
或倍()
,除非有没有好。最终回退,但不能满足您当前的需求,是
Do not use ftime()
or times()
unless there is nothing better. The ultimate fallback, but not meeting your immediate requirements, is
-
时间()
- 一个第二项决议
time()
- one second resolution
这篇关于时钟()在time.h中precision的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!