作者allanyh (allan)
看板C_and_CPP
标题Re: [问题] unix下 c++程式 CLOCKS_PER_SEC 与CLK_ …
时间Sun Feb 15 01:33:41 2009
※ 引述《WalkingIce ( 杀手哥吉拉 13)》之铭言:
: ※ 引述《allanyh (allan)》之铭言:
: : 不好意思,这样我又有点迷糊了,因为我的程式不可能跑100秒啊,不到一秒钟就run出来了
: : 到底哪个才是把clock换算成秒数
: 你要算程式跑了多久是吗?
谢谢,
不过我的gcc compiler warning,而且出来start=-1,finish=-1结果都是0
warning: implicit declaration of function `int times(...)'
warning: implicit declaration of function `int sysconf(...)'
如果是time()函数,那个只能精确到秒不是吗??
gettimeofday可不可以解释详细一点.我还看不大懂如何用
谢谢您费心ㄧ再解答我的疑惑,感谢
: 1 #include <stdlib.h>
: 2 #include <stdio.h>
: 3 #include <time.h>
: 4
: 5 int main() {
: 6 clock_t start,finish;
: 7 start = times(NULL);
: 8 int i = 0, temp = 0;
: 9 for(i = 0; i < 100000000 ; i++) {
: 10 // do nothing
: 11 }
: 12 sleep(2);
: 13 finish = times(NULL);
: 14 float elapsed = (float)(finish - start) / sysconf(2);
: 15 printf("Start: %d\nFinish: %d\n", start, finish);
: 16 printf("Passed:%f\n", elapsed);
: 17 }
: $ gcc clock.c && ./a.out
: Start: 1728665314
: Finish: 1728665543
: Passed:2.290000
: 或着你可以试试 gettimeofday,前几天刚拿来用
: // Borrowed this part of code from ecore_time.c of Enlightenment project.
: /* FIXME: clock_gettime() is an option... */
: /**
: * Retrieves the current system time as a floating point value in seconds.
: * @return The number of seconds since 12.00AM 1st January 1970.
: */
: double dindin_time_get(void) {
: struct timeval timev;
: gettimeofday(&timev, NULL);
: return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
: }
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.169.231.68
1F:推 alongalone:改用gettimeofday可能比较符合你的需求 02/15 16:31
2F:→ alongalone:用clock(),最好是用回圈包起来算,我记得单位是ms 02/15 16:32
感谢,解决了:)用W兄的gettimeofday的用法跟第一次
duration1 = (double)(finish - start) / CLOCKS_PER_SEC;
的时间是差不多的,所以我的SERVER除CLK_TCK似乎不是得到秒数
而是应该除CLOCKS_PER_SEC
※ 编辑: allanyh 来自: 210.240.176.185 (02/16 21:38)