Boost库中的xtime是一个高精度计时器,可以用来测量代码执行时间等。在使用xtime之前需要了解一些基本概念和API函数,下面就来介绍一下。
xtime的基本概念:
1.秒(second),我们常用的时间单位;
2.纳秒(nanosecond),时间的最小单位,等于10的负九次方秒,即1e-9秒。
xtime API函数:
1.boost::xtime_get():
该函数返回当前精确时间。返回值为boost::xtime结构体,成员sec表示秒数,nsec表示纳秒数。
2.boost::xtime_now():
该函数返回当前精确时间,与boost::xtime_get()功能相同,但它使用操作系统的高分辨率计时器,因此精度更高。
下面是一个简单的测试程序,用于演示如何使用xtime测量代码的运行时间:
#include
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::posix_time::ptime start_time = boost::posix_time::microsec_clock::local_time();