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.

  • 相关阅读:
    django wsgi nginx 配置
    supervisor error: <class 'socket.error'>, [Errno 110]
    gunicorn 启动无日志
    获取windows 网卡GUID和ip信息
    亚马逊EC2根硬盘空间扩容
    pypcap 安装
    mysql 1709: Index column size too large. The maximum column size is 767 bytes.
    mysql死锁检查
    D3.js画思维导图(转)
    用D3.js画树状图
  • 原文地址:https://www.cnblogs.com/sssblog/p/11305083.html
Copyright © 2011-2022 走看看