一、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)有多少个时钟计时单元
1 2 3 4 5 6 7 8 9 10 11 | #include <time.h> //引入头文件
voidtime1()
{
clock_tstart, 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。
1 2 3 4 5 6 7 8 9 10 11 | #include <windows.h> //引入头文件
voidtime2()
{
DWORDt1, t2;
t1 = GetTickCount();
fun();//需计时的函数
t2 = GetTickCount();
cout <<"elapsed time in GetTickCount = "<<double(t2 - t1) <<"ms"<< endl;
}
|
三、高精度时控函数QueryPerformanceCounter()——微妙级
原理:这里使用高精度时控函数QueryPerformanceFrequency(),QueryPerformanceCounter()
它们是两个精度很高的函数,精度单位为微秒。使用QueryPerformanceCounter()即可获取这个高精度计时器的值,但是由于机器的原因,它们实际上的精度会大幅度受到机器运作的影响,则必须向系统查询它们确切的运作频率QueryPerformanceFrequency()函数提供了这个功能,可以通过这一个函数来获取高精度计时器的运作频率(在一秒钟之内它的运作次数),用两次调用QueryPerformanceCounter()函数的结果做差除以QueryPerformanceFrequency()的运作频率即可求出在两次“时间获取”之间所经过的时间。在其中放入想要测量时间的算法代码,就可以得知算法的运行时长。
精度:计算机获取硬件支持,精度比较高,可以通过它判断其他时间函数的精度范围。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //QueryPerformanceCounter()是一个Windows API,所需头文件为<windows.h>
#include <windows.h> //引入头文件
voidtime3()
{
LARGE_INTEGER t1, t2, tc;
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
fun();//需计时的函数
QueryPerformanceCounter(&t2);
doubletime= (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart;
cout <<"elapsed time in QuadPart = "<<time* 1000 <<"ms"<< endl;
}
|
四、高精度计时chrono函数——纳妙级
C++11 中的 chrono 库提供了精度最高为纳秒级的计时接口。由于是标准库中提供的功能,所以可以很好地跨平台使用。
下面来看一段使用 chrono 进行高精度计时的示例代码:
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 <iostream>
#include <chrono>
voidtime4()
{
// 计时开始时间点
// 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.
五、几种计时比较
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <iostream>
#include <chrono>
#include <thread>
#include <time.h>
#include <windows.h>
usingnamespacestd;
voidfun()
{
// 睡眠100ms
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
voidcompareTime()
{
LARGE_INTEGER t1, t2, tc;
QueryPerformanceFrequency(&tc);
clock_tstart, end;
DWORDt11, 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();
doubletime= (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所指的结构中.
1 2 3 4 5 6 7 8 9 10 | //timeval结构定义为:
structtimeval{
longtv_sec;/*秒*/
longtv_usec;/*微秒*/
};
//timezone 结构定义为:
structtimezone{
inttz_minuteswest;/*和Greenwich 时间差了多少分钟*/
inttz_dsttime;/*日光节约时间的状态*/
};
|
这个函数获取从1970年1月1日到现在经过的时间和时区(UTC时间),(按照linux的官方文档,时区已经不再使用,正常应该传NULL)。
调用代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <sys/time.h> //引入头文件
intmain()
{
structtimeval t1,t2;
doubletimeuse;
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)
}
|
参考文献