首页 > 其他 > 详细

boost timer

时间:2019-08-05 19:59:44      阅读:142      评论:0      收藏:0      [点我收藏+]

Boost.Timer provides clocks to measure code performance. At first, it may seem like this library competes with Boost.Chrono. However, while Boost.Chrono provides clocks to measure arbitrary periods, Boost.Timer measures the time it takes to execute code. Although Boost.Timer uses Boost.Chrono, when you want to measure code performance, you should use Boost.Timer rather than Boost.Chrono.

1. cpu_timer

#include <boost/timer/timer.hpp>
#include <iostream>
#include <cmath>

using namespace boost::timer;

int main()
{
  cpu_timer timer;

  for (int i = 0; i < 1000000; ++i)
    std::pow(1.234, i);
  std::cout << timer.format() << std::endl;
  return 0;
}

Measurement starts when boost::timer::cpu_timer is instantiated. You can call the member function format() at any point to get the elapsed time.

输出为:

0.057313s wall, 0.050000 user + 0.000000s system = 0.050000 CPU (87.2%)

The wall time is the time which passes according to a wall clock. The CPU time says how much time the program spent executing code. CPU time is divided between time spent in user space and time spent in kernel space.

2. stopping and resuming timers

#include <boost/timer/timer.hpp>
#include <iostream>
#include <cmath>

using namespace boost::timer;

int main()
{
  cpu_timer timer;

  for (int i = 0; i < 1000000; ++i)
    std::pow(1.234, i);
  std::cout << timer.format() << std::endl;

  timer.stop();

  for (int i = 0; i < 1000000; ++i)
    std::pow(1.234, i);
  std::cout << timer.format() << std::endl;

  timer.resume();

  for (int i = 0; i < 1000000; ++i)
    std::pow(1.234, i);
  std::cout << timer.format() << std::endl;
  return 0;
}

boost::timer::cpu_timer provides the member functions stop() and resume(), which stop and resume timers. boost::timer::cpu_timer also provides a member function start(). If you call start(), the timer restarts from zero.

3. getting wall and cpu time as a tuple

#include <boost/timer/timer.hpp>
#include <iostream>
#include <cmath>

using namespace boost::timer;

int main()
{
  cpu_timer timer;

  for (int i = 0; i < 1000000; ++i)
    std::pow(1.234, i);

  cpu_times times = timer.elapsed();
  std::cout << times.wall << std::endl;
  std::cout << times.user << std::endl;
  std::cout << times.system << std::endl;
  return 0;
}

boost::timer::cpu_timer provides the member function elapsed(). elapsed() returns a tuple of type boost::timer::times. This tuple has three member varibles: wall, user and system. These member variables contain the wall and CPU times in nanoseconds. boost::timer::times provides the member function clear() to set wall, user and system to 0.

4. auto_cpu_timer

#include <boost/timer/timer.hpp>
#include <cmath>

using namespace boost::timer;

int main()
{
  auto_cpu_timer timer;

  for (int i = 0; i < 1000000; ++i)
    std::pow(1.234, i);
  return 0;
}

The destructor of this class stops measuring time and writes the time to the standard output stream.

boost timer

原文:https://www.cnblogs.com/sssblog/p/11305083.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!