zoukankan      html  css  js  c++  java
  • boost timer

    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.

  • 相关阅读:
    011-通过网络协议解析网络请求-DNS-ARP-TCPIP
    010-HTTP协议
    009-DNS域名解析系统
    008-ICMP协议(网络控制文协议)
    007-IP报文协议
    007-排序算法-堆排序
    006-排序算法-希尔排序
    007-Linux 查看端口
    005-排序算法-归并排序
    004-排序算法-选择排序
  • 原文地址:https://www.cnblogs.com/sssblog/p/11305083.html
Copyright © 2011-2022 走看看