clock()函数返回值溢出问题

manrenmanren 2009-05-21 04:57:39
startCPU = clock();
....
stopCPU = clock();
return (stopCPU-startCPU)/CLOCKS_PER_SEC;

为什么 有的时候返回负值呢,如何解决呢?谢谢!!!

...全文
799 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
manrenmanren 2009-05-21
  • 打赏
  • 举报
回复
1个半小时以上吧。
kingstarer 2009-05-21
  • 打赏
  • 举报
回复
你的程序跑多久啊?
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 manrenmanren 的回复:]
单位 clock()函数返回的是什么?
毫秒么?
[/Quote]

返回进程开始后的时钟滴答数
manrenmanren 2009-05-21
  • 打赏
  • 举报
回复
对啊 我知道啊
可是如何处理溢出呢?
goodname 2009-05-21
  • 打赏
  • 举报
回复

clock function

clock_t clock ( void );



<ctime>

Clock program

Returns the number of clock ticks elapsed since the program was launched.

The macro constant expression CLOCKS_PER_SEC specifies the relation between a clock tick and a second (clock ticks per second).

The initial moment of reference used by clock as the beginning of the program execution may vary between platforms. To calculate the actual processing times of a program, the value returned by clock should be compared to a value returned by an initial call to clock.

Parameters
(none)

Return Value
The number of clock ticks elapsed since the program start.

On failure, the function returns a value of -1.

clock_t is a type defined in <ctime> to some type capable of representing clock tick counts and support arithmetical operations (generally a long integer).
manrenmanren 2009-05-21
  • 打赏
  • 举报
回复
单位 clock()函数返回的是什么?
毫秒么?
goodname 2009-05-21
  • 打赏
  • 举报
回复
溢出还是比较难的。

也许时钟系统有问题?可能性比较的小。
fly_new 2009-05-21
  • 打赏
  • 举报
回复
单位是啥?32位的有符号数,如果是ms,也要20多天才溢出呢!
manrenmanren 2009-05-21
  • 打赏
  • 举报
回复
是的 溢出了
返回值是个负数

  • 打赏
  • 举报
回复
clock_t clock(void) ;
  这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对它的定义:
  #ifndef _CLOCK_T_DEFINED
  typedef long clock_t;
  #define _CLOCK_T_DEFINED
  #endif

你的时钟数溢出了?
manrenmanren 2009-05-21
  • 打赏
  • 举报
回复
本来是clock_t 但是会溢出
clock_t 应该是long

我改成了double 还是溢出
liliangbao 2009-05-21
  • 打赏
  • 举报
回复
startCPU 是什么类型?
用clock_t或者unsigned int
lingyin55 2009-05-21
  • 打赏
  • 举报
回复
startCPU 是什么类型?用long
lsldd 2009-05-21
  • 打赏
  • 举报
回复
还真没遇到过!顶

70,028

社区成员

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

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