C++计算一个程序运行时间,精确到毫秒
用clock_t声明两个变量start,finish;start=clock()//运行一个函数finish=clock()得到的finish-start是执行这个函数所...
用clock_t声明两个变量start,finish;
start = clock()
//运行一个函数
finish = clock()
得到的 finish-start 是执行这个函数所用的时间吧 单位是什么啊?是秒呢?毫秒呢?还是微秒呢?有的地方再除以个 CLK_TCK 或CLOCKS_PER_SEC是什么意思呢?怎样得到毫秒或微妙的精确度啊? 展开
start = clock()
//运行一个函数
finish = clock()
得到的 finish-start 是执行这个函数所用的时间吧 单位是什么啊?是秒呢?毫秒呢?还是微秒呢?有的地方再除以个 CLK_TCK 或CLOCKS_PER_SEC是什么意思呢?怎样得到毫秒或微妙的精确度啊? 展开
3个回答
展开全部
clock()返回的是CPU时钟计时单元,而CLOCKS_PER_SEC它用来表示一秒钟会有多少个时钟计时单元,所以正确的运行时间是(finish-start)/CLOCKS_PER_SEC,这样就能得到执行了多少秒,要得到毫秒的话再乘以1000.0,微妙再乘以1000.0
追问
如果要得到毫秒 是直接finish-start还是(finish-start)/CLOCKS_PER_SEC*1000呢?怎么和楼下有点不一样
追答
这个看CLOCKS_PER_SEC的值了,VC中确实定义了CLOCKS_PER_SEC的值是1000,所以
finish-start或者(finish-start)/CLOCKS_PER_SEC*1000.0都能够得到毫秒
2011-12-02
展开全部
单位是毫秒
除以个 CLK_TCK 或CLOCKS_PER_SEC是为了得到秒值(秒=毫秒/1000)
在VC6.0中有关于CLK_TCK的说明:
#if !__STDC__ || defined(_POSIX_)
#define CLK_TCK CLOCKS_PER_SEC
_CRTIMP extern int daylight;
_CRTIMP extern long timezone;
_CRTIMP extern char * tzname[2];
_CRTIMP void __cdecl tzset(void);
#endif
在VC++6.0中类似的还有CLOCKS_PER_SEC 。其值为1000。
#define CLOCKS_PER_SEC 1000
因此VC6.0中CLK_TCK的值和CLOCKS_PER_SEC 的值是1000。
除以个 CLK_TCK 或CLOCKS_PER_SEC是为了得到秒值(秒=毫秒/1000)
在VC6.0中有关于CLK_TCK的说明:
#if !__STDC__ || defined(_POSIX_)
#define CLK_TCK CLOCKS_PER_SEC
_CRTIMP extern int daylight;
_CRTIMP extern long timezone;
_CRTIMP extern char * tzname[2];
_CRTIMP void __cdecl tzset(void);
#endif
在VC++6.0中类似的还有CLOCKS_PER_SEC 。其值为1000。
#define CLOCKS_PER_SEC 1000
因此VC6.0中CLK_TCK的值和CLOCKS_PER_SEC 的值是1000。
追问
意思是我要精确到毫秒 直接finish-start就可以了吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Return Value
clock returns the number of clock ticks of elapsed processor time. The returned value is the product of the amount of time that has elapsed since the start of a process and the value of the CLOCKS_PER_SEC constant. If the amount of elapsed time is unavailable, the function returns –1, cast as a clock_t.
Remarks
The clock function tells how much processor time the calling process has used. The time in seconds is approximated by dividing the clock return value by the value of the CLOCKS_PER_SEC constant. In other words, clock returns the number of processor timer ticks that have elapsed. A timer tick is approximately equal to 1/CLOCKS_PER_SEC second. In versions of Microsoft C before 6.0, the CLOCKS_PER_SEC constant was called CLK_TCK.
msdn上的解释。。
clock returns the number of clock ticks of elapsed processor time. The returned value is the product of the amount of time that has elapsed since the start of a process and the value of the CLOCKS_PER_SEC constant. If the amount of elapsed time is unavailable, the function returns –1, cast as a clock_t.
Remarks
The clock function tells how much processor time the calling process has used. The time in seconds is approximated by dividing the clock return value by the value of the CLOCKS_PER_SEC constant. In other words, clock returns the number of processor timer ticks that have elapsed. A timer tick is approximately equal to 1/CLOCKS_PER_SEC second. In versions of Microsoft C before 6.0, the CLOCKS_PER_SEC constant was called CLK_TCK.
msdn上的解释。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询