在linux下clock()函数始终返回0???

leehq 2007-04-11 01:53:26
看看这段代码:

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

int main()
{
clock_t now,start_time = clock();
do
{
printf( "wait...\n" );
sleep( 1 );
now = clock();
printf( "%d-%d=%d\n", now, start_time, (now - start_time) / CLOCKS_PER_SEC );
}
while( clock() - start_time < CLOCKS_PER_SEC * 10 );

return 0;
}

结果:
wait...
0-0=0
wait...
0-0=0
wait...
0-0=0
wait...
0-0=0
wait...
0-0=0


...全文
6462 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
match5 2010-09-16
  • 打赏
  • 举报
回复
我也遇到同样的问题
#include <iostream>

using namespace std;

int main()
{
int m,n,r;
clock_t start, finish;

cout<<"m=";
cin>>m;
cout<<endl<<"n=";
cin>>n;

start=clock();
for(;;m=n,n=r)
{
r=m%n;
if(!r)
{
cout<<"result:"<<n<<endl;
break;
}
}
finish=clock();

cout<<"time:"<<finish-start<<endl;

return 0;
}
不管用输入什么数据 最后的时间总是0
contain_universe 2007-04-25
  • 打赏
  • 举报
回复
代码时间太短,搞个时间复杂度高的试试
leehq 2007-04-25
  • 打赏
  • 举报
回复
看来也就是这样了···
leehq 2007-04-25
  • 打赏
  • 举报
回复
代码时间太短,搞个时间复杂度高的试试
-----------------------------------------
真的很短吗?
leehq 2007-04-23
  • 打赏
  • 举报
回复
还有人来说说看,有什么更好的计时办法?
leehq 2007-04-20
  • 打赏
  • 举报
回复
mathe()
强啊,用汇编指令
mathe 2007-04-19
  • 打赏
  • 举报
回复
我给个程序:
#include <stdio.h>

#define get_timer(val) __asm__ __volatile__ ("rdtsc":"=A"(val))
#define TICK_PER_SECOND 3600000000ULL

int main()
{
unsigned long long now, start_time;
get_timer(start_time);
do{
printf("Wait..\n");
sleep(1);
get_timer(now);
printf("%llu-%llu=%llu\n", now, start_time, (now-start_time+TICK_PER_SECOND/2)/TICK_PER_SECOND);
}while(now-start_time<10*TICK_PER_SECOND);
return 0;
}

不过这里TICK_PER_SECOND要自己设置,同机器主频有关系,比如我的机器是3.6G.
不过对于多CPU的机器,结果就不一定完全正确
leehq 2007-04-19
  • 打赏
  • 举报
回复
还有人来说说看,有什么更好的计时办法?
lbaby 2007-04-18
  • 打赏
  • 举报
回复
试了一下,不是输出格式的问题,原因是clock给出的是程序占用的CPU时间



clock(3C) clock(3C)

NAME
clock() - report CPU time used

SYNOPSIS
#include <time.h>

clock_t clock(void);

DESCRIPTION
clock() returns the amount of CPU time (in microseconds) used since
the first call to clock(). The time reported is the sum of the user
and system times of the calling process and its terminated child
processes for which it has executed wait() , system() or pclose() (see
wait(2) , system(3S), and popen(3S)). To determine the time in
seconds, the value returned by clock() should be divided by the value
of the macro CLOCKS_PER_SEC.

The resolution of the clock varies, depending on the hardware and on
software configuration.

If the processor time used is not available or its value cannot be
represented, the function returns the value (clock_t)-1.



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

int main()
{
clock_t now,start_time = clock();
int i=0;
int d = 0;
do
{
printf( "wait...\n" );
sleep( 1 );
for (i=0; i < 100000000; i++){
d+=i;
}


now = clock();
printf( "%d-%d=%d\n", now, start_time, (now - start_time) / CLOCKS_PER_SEC );
printf( "%ld-%ld=%f\n", now, start_time, (double)(now - start_time) / CLOCKS_PER_SEC );
}
while( clock() - start_time < CLOCKS_PER_SEC * 10 );
printf("%d",d);
return 0;
}
leehq 2007-04-18
  • 打赏
  • 举报
回复
每人了吗?
leehq 2007-04-18
  • 打赏
  • 举报
回复
还有人来说说看,有什么更好的计时办法?
leehq 2007-04-16
  • 打赏
  • 举报
回复
lbaby(春天来了...)
--------------------------
看来不明白的人不止你一个····
leehq 2007-04-12
  • 打赏
  • 举报
回复
还有高手来指点一下吗?
lockhall 2007-04-12
  • 打赏
  • 举报
回复
到处见到星星级人物!嗬嗬
lbaby 2007-04-12
  • 打赏
  • 举报
回复
上边的兄弟们已经说的很明白了,你的输出格式有问题
thinkinnight 2007-04-11
  • 打赏
  • 举报
回复
哦,没有linux,只用VC测了一下,是我想当然了
yzx1983 2007-04-11
  • 打赏
  • 举报
回复
用gettimeofday吧
结构而非整数这一点点的不方便,不应该成为不使用它的理由。
leehq 2007-04-11
  • 打赏
  • 举报
回复
yzx1983(捕风捉影):
我知道用time和ftime来计时,但是它们的使用好像不是很方便,因为ftime得到的是一个结构,而不是表示毫秒的整数。
yzx1983 2007-04-11
  • 打赏
  • 举报
回复
time

NAME
time - get time in seconds

SYNOPSIS
#include <time.h>

time_t time(time_t *t);

DESCRIPTION
time returns the time since the Epoch (00:00:00 UTC, January 1, 1970),
measured in seconds.

If t is non-NULL, the return value is also stored in the memory pointed
to by t.

RETURN VALUE
On success, the value of time in seconds since the Epoch is returned.
On error, ((time_t)-1) is returned, and errno is set appropriately.

ERRORS
EFAULT t points outside your accessible address space.

NOTES
POSIX.1 defines seconds since the Epoch as a value to be interpreted as
the number of seconds between a specified time and the Epoch, according
to a formula for conversion from UTC equivalent to conversion on the na
basis that leap seconds are ignored and all years divisible by 4 are
leap years. This value is not the same as the actual number of seconds
between the time and the Epoch, because of leap seconds and because
clocks are not required to be synchronised to a standard reference.
The intention is that the interpretation of seconds since the Epoch
values be consistent; see POSIX.1 Annex B 2.2.2 for further rationale.

CONFORMING TO
SVr4, SVID, POSIX, X/OPEN, BSD 4.3
Under BSD 4.3, this call is obsoleted by gettimeofday(2). POSIX does
not specify any error conditions.

SEE ALSO
ctime(3), date(1), ftime(3), gettimeofday(2)
leehq 2007-04-11
  • 打赏
  • 举报
回复
thinkinnight(逍遥):
我是在linux编程,sleep的单位是秒。
-----------------------------------------

我知道问题出在sleep上了,在linux下sleep不使用cpu时间,所以clock的结果都是0。

我现在想让程序等待一段时间,然后再继续执行,不用clock的话还有什么别的办法吗?
哪位大虾来指点一下?
加载更多回复(7)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧