C++常用计时函数记录

time()

time()获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数,精确程度很低

#include<ctime>
using namespace std;

int main(){
	time_t start,end;
	start=time(NULL);		//程序开始计时
	int tmp=0;
	for(int i=1;i<=1e8;i++)
		tmp++;
	end=time(NULL);		//程序结束用时
	int timeuse=end-start;
	cout<<"Total time:"<<timeuse<<endl;		//s为单位
	return 0;

clock()

clock()函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock),在处理多线程时很不理想

常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元

#include<ctime>
using namespace std;

int main(){
	clock_t start,end;
	double timeuse;
	start=clock();		//程序开始计时
	int tmp=0;
	for(int i=1;i<=1e8;i++)
		tmp++;
	end=clock();		//程序结束用时
	double timeuse=(double)(end-start)/CLOCKS_PER_SEC;
	cout<<"Total time:"<<timeuse<<endl;		//s为单位
	cout<<"Total time:"<<timeuse*1000<<"ms"<<endl;	//ms为单位
	return 0;

gettimeofday()

linux环境下的计时函数,int gettimeofday ( struct timeval * tv , struct timezone * tz ),gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中,是Linux系统提供的函数

//timeval结构定义为:
struct timeval{
    long tv_sec; /*秒*/
    long tv_usec; /*微秒*/
};

 //timezone 结构定义为:
struct timezone{
    int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
    int tz_dsttime; /*日光节约时间的状态*/
};
    
#include <sys/time.h>
using namespace std;

int main(){
	struct timeval t1,t2;
	double timeuse;
	gettimeofday(&t1,NULL);		//程序开始计时
	int tmp=0;
	for(int i=1;i<=1e8;i++)
		tmp++;
	gettimeofday(&t2,NULL);		//程序结束用时
	timeuse=t2.tv_sec - t1.tv_sec + (t2.tv_usec - t1.tv_usec)/1000000.0;
	printf("time_use is %.10f\n",time_use);
	return 0;

timeGetTime()

timeGetTime()函数以毫秒计的系统时间。该时间为从系统开启算起所经过的时间,是windows api

#include <Windows.h>

int main(){
    DWORD t1,t2;
    double timeuse;
    t1 = timeGetTime();
    int tmp=0;
	for(int i=1;i<=1e8;i++)
		tmp++;
    t2 = timeGetTime();
    timeuse = (t2-t1)*1.0/1000;
    printf("Use Time:%f\n",timeuse);
}

C++11 chrono库

C++11中的chrono库,可以用来写计时器,跨平台,跨编译器


//timer.h
#ifndef W_TIMER_H
#define W_TIMER_H

#include <fstream>
#include <string>
#include <chrono>

class Timer {
public:

	Timer() :_name("Default") {
		restart();
	}

	explicit Timer(const std::string &name) :_name(name) {
		restart();
	}

	/**
	* 启动计时
	*/
	inline void restart() {
		_start_time = std::chrono::steady_clock::now();
	}

	/**
	* 结束计时
	* @return 返回ms数
	*/
	inline double elapsed(bool restart = false) {
		_end_time = std::chrono::steady_clock::now();
		std::chrono::duration<double> diff = _end_time - _start_time;
		if (restart)
			this->restart();
		return diff.count() * 1000;
	}

	/**
	* 打印时间并重启计时器
	* @param tip 提示
	* @param logfile 打印log到logfile指定的文件
	*/
	void rlog(const std::string &logfile = "", const std::string &tip = "") {
		log(true, logfile, tip);
	}

	/**
	* 打印时间
	* @param reset 输出之后是否重启计时器,true重启,false不重启
	* @param tip 输出提示
	* @param logfile 打印log到logfile指定的文件
	*/
	void log(bool reset = false, const std::string &logfile = "",
		const std::string &tip = "") 
	{
		std::ofstream fout;
		if (logfile.length() == 0)
			fout.open("./Timerlog/" + _name,std::ios::app,0);
		else
			fout.open("./Timerlog/" + logfile,std::ios::app,0);
		if (tip.length() > 0)
			fout << tip <<":"<< elapsed() << "ms"<<std::endl;
		else
			fout << elapsed() << ",";

		if (reset)
			this->restart();
	}


private:
	std::chrono::steady_clock::time_point _start_time;
	std::chrono::steady_clock::time_point _end_time;
	std::string _name;
}; // timer

#endif //W_TIMER_H
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值