当前位置: 代码网 > it编程>编程语言>C/C++ > C++高精度计时的几种方法总结(测试函数运行时间)

C++高精度计时的几种方法总结(测试函数运行时间)

2024年09月23日 C/C++ 我要评论
一、clock()函数——毫妙级c系统调用方法,所需头文件ctime/time.h,即windows和linux都可以使用。1、clock()返回类型为clock_t类型2、c

一、clock()函数——毫妙级

c系统调用方法,所需头文件ctime/time.h,即windows和linux都可以使用。

1、clock()返回类型为clock_t类型

2、clock_t实际为long 类型, typedef long clock_t

3、clock() 函数,返回从 开启这个程序进程 到 程序中调用clock()函数 时之间的cpu时钟计时单元(clock tick)数(挂钟时间),返回单位是毫秒

4、可以用常量clocks_per_sec, 这个常量表示每一秒(per second)有多少个时钟计时单元

#include <time.h>   //引入头文件
void time1()
{
	clock_t start, end;
	start = clock();

	fun();  //需计时的函数

	end = clock();  
	cout << "elapsed time in clock = " << double(end - start) << "ms" << endl;  
}

二、gettickcount()函数(精度16ms左右)——毫妙级

gettickcount()是一个windows api,所需头文件为<windows.h>。是通过计算从函数开始运行计时,直到函数运行结束所求出函数的运行时间。它返回从操作系统启动所经过的毫秒数,

此处需要注意的是,这个函数所求的的运行时间并非准确运行时间,不过相对来说比较准确,它的精度和cpu有关,一般精度在16ms左右,由于gettickcount()返回值以32位的双字类型dword存储,所以它的存储最大值是(2^32-1) ms约为49.71天,一旦一个程序运行时间超过这个值,这个数字就会归为0。

#include <windows.h>   //引入头文件
void time2()
{
	dword t1, t2;
	t1 = gettickcount();

	fun();  //需计时的函数

	t2 = gettickcount();
	cout << "elapsed time in gettickcount = " << double(t2 - t1) << "ms" << endl;
}

三、高精度时控函数queryperformancecounter()——微妙级

原理:这里使用高精度时控函数queryperformancefrequency(),queryperformancecounter()
它们是两个精度很高的函数,精度单位为微秒。使用queryperformancecounter()即可获取这个高精度计时器的值,但是由于机器的原因,它们实际上的精度会大幅度受到机器运作的影响,则必须向系统查询它们确切的运作频率queryperformancefrequency()函数提供了这个功能,可以通过这一个函数来获取高精度计时器的运作频率(在一秒钟之内它的运作次数),用两次调用queryperformancecounter()函数的结果做差除以queryperformancefrequency()的运作频率即可求出在两次“时间获取”之间所经过的时间。在其中放入想要测量时间的算法代码,就可以得知算法的运行时长。

精度: 计算机获取硬件支持,精度比较高,可以通过它判断其他时间函数的精度范围。

//queryperformancecounter()是一个windows api,所需头文件为<windows.h>
#include <windows.h>   //引入头文件
void time3()
{
	large_integer t1, t2, tc;
	queryperformancefrequency(&tc);
	queryperformancecounter(&t1);

	fun();  //需计时的函数

	queryperformancecounter(&t2);
	double time = (double)(t2.quadpart - t1.quadpart) / (double)tc.quadpart;
	cout << "elapsed time in quadpart = " << time * 1000 << "ms" << endl;
}

四、高精度计时chrono函数——纳妙级

c++11 中的 chrono 库提供了精度最高为纳秒级的计时接口。由于是标准库中提供的功能,所以可以很好地跨平台使用。

下面来看一段使用 chrono 进行高精度计时的示例代码:

#include <iostream>
#include <chrono>
void time4()
{
	// 计时开始时间点
	// chrone 中常用的时钟类:
	// 		- std::chrono::high_resolution_clock
	//   	- std::chrono::system_clock
	//      - std::chrono::steady_clock
	// 三种时钟类有一些区别,其中 high_resolution_clock 精度最高
	auto start = std::chrono::high_resolution_clock::now();

	// 要计时的代码段
	fun();  
	// 计时结束时间点
	auto end = std::chrono::high_resolution_clock::now();

	// 计算运行时间, 时间单位:
	//      - std::chrono::seconds
	//      - std::chrono::milliseconds
	//      - std::chrono::microseconds
	//      - std::chrono::nanoseconds
	auto duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start);

	// 输出时间(给定时间单位)
	cout << "elapsed time in chrono = " << duration.count()/1000.0 << "ms" << endl;
} 

其中,microseconds 表示微妙。除此之外,还有五种时间单位:hours, minutes, seconds, milliseconds, nanoseconds.

五、几种计时比较

#include <iostream>
#include <chrono>
#include <thread>
#include <time.h>
#include <windows.h>

using namespace std;

void fun()
{
	// 睡眠100ms
	std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
void comparetime()
{
	large_integer t1, t2, tc;
	queryperformancefrequency(&tc);
	
	
	clock_t start, end;

	dword t11, t12;
	start = clock();
	t11 = gettickcount();
	queryperformancecounter(&t1);
	auto start1 = std::chrono::high_resolution_clock::now();

	fun();  //需计时的函数

	end = clock();
	t12 = gettickcount();
	queryperformancecounter(&t2);
	auto end1 = std::chrono::high_resolution_clock::now();
	double time = (double)(t2.quadpart - t1.quadpart) / (double)tc.quadpart;
	auto duration = std::chrono::duration_cast<std::chrono::microseconds> (end1 - start1);

	cout << "elapsed time in clock = " << double(end - start) << "ms" << endl;
	cout << "elapsed time in gettickcount = " << double(t12 - t11) << "ms" << endl;
	cout << "elapsed time in quadpart = " << time * 1000 << "ms" << endl;
	cout << "elapsed time in chrono = " << duration.count() / 1000.0 << "ms" << endl;
}

六、linux下的计时函数gettimeofday()-未测试

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

//timeval结构定义为:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
//timezone 结构定义为:
struct timezone{
int tz_minuteswest; /*和greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};

这个函数获取从1970年1月1日到现在经过的时间和时区(utc时间),(按照linux的官方文档,时区已经不再使用,正常应该传null)。

调用代码:

#include <sys/time.h>   //引入头文件
int main()
{
    struct timeval t1,t2;
    double timeuse;
    gettimeofday(&t1,null);
 
    fun();
 
    gettimeofday(&t2,null);
    timeuse = (t2.tv_sec - t1.tv_sec) + (double)(t2.tv_usec - t1.tv_usec)/1000000.0;
 
    cout<<"time = "<<timeuse<<endl;  //输出时间(单位:s)
}

参考文献

c++下四种常用的程序运行时间的计时方法总结

c++中几种测试程序运行时间的方法

到此这篇关于c++高精度计时的几种方法的文章就介绍到这了,更多相关c++高精度计时方法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com