计时器的 time_t 和 clock_t 两种实现方法

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

#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 类型的实现方式是

#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秒,明显不对。造成这种结果的原因暂时还不清楚,或许是因为算法执行过程中在频繁调用其他外部程序来获得一些计算结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值