boost库中的timer类能够为程序员提供毫秒级别的操作精度和操作函数,它是一个小型的计时器,可以用来测量时间的流逝。
timer类位于boost命名空间下,使用时需要包含头文件:#include <boost\timer.hpp>
下面是timer使用示例:
timer _timer;
cout << "可度量的最大时间(s):" << _timer.elapsed_max() << endl;
cout << "可度量的最小时间(s):" << _timer.elapsed_min() << endl;
cout << "已经流逝的时间(s):" << _timer.elapsed() << endl;
输出可以参考下面的结果:
可度量的最大时间:2.14748e+06
可度量的最小时间:0.001
已经流逝的时间:0.195
由于机器的不同,其中elapsed_max()返回的结果也不同,这一点相信大家都有所了解,这里的结果仅供参考。
timer类是很短小精悍的一个类,它的代码行数很少,而且加上构造函数也就五个函数。这里贴出它的源码:
// boost timer.hpp header file ---------------------------------------------//
// Copyright Beman Dawes 1994-99. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org/libs/timer for documentation.
// Revision History
// 01 Apr 01 Modified to use new <boost/limits.hpp> header. (JMa