计时器的time_t和clock_t 的两种实现方法(推荐)

2025-05-27 0 56

想给自己初步完成的相空间搜索算法计算一下运行时间,于是尝试了如下使用 time_t 类型的方式

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27
#include <stdlib.h>

#include <iostream>

#include <time.h>

#include "StateFunctions.h"

using namespace std;

int main(int argc, char** argv)

{

time_t start, finish;

time(&start);

StateFunctions testobj(22, 22);

testobj.TEST();

testobj.TEST();

testobj.FillRandomDets(200);

testobj.evolute(1000, 0.9);

cout << "--------------------------------------------" << endl;

time(&finish);

double duration = difftime(finish, start);

cout << "--> time: " << duration << " s" << endl;

cout << "--------------------------------------------" << endl;

return 0;

}

这种实现方式可以正确计算出算法的核心部分耗费了234秒的 walltime。在此之前尝试的使用 clock_t 类型的实现方式是

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
#include <iostream>

#include <time.h>

#include "StateFunctions.h"

using namespace std;

int main(int argc, char** argv)

{

clock_t start, finish;

start = clock();

StateFunctions testobj(22, 22);

testobj.TEST();

testobj.TEST();

testobj.FillRandomDets(200);

testobj.evolute(1000, 0.9);

cout << "--------------------------------------------" << endl;

finish = clock();

double duration = (double)(finish - start) / CLOCKS_PER_SEC;

cout << "--> time: " << duration << " s" << endl;

cout << "--------------------------------------------" << endl;

return 0;

}

这段代码得到的运行时间只有11秒,明显不对。造成这种结果的原因暂时还不清楚,或许是因为算法执行过程中在频繁调用其他外部程序来获得一些计算结果。

以上就是小编为大家带来的计时器的time_t和clock_t 的两种实现方法(推荐)全部内容了,希望大家多多支持快网idc~

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 计时器的time_t和clock_t 的两种实现方法(推荐) https://www.kuaiidc.com/74711.html

相关文章

猜你喜欢
发表评论
暂无评论