boost::progress_timer 与 boost::progress_display

1、boost::progress_timer

作用与使用:

继承自 boost::timer ,作用相同,只是其在析构函数中会输出流逝的时间,所以在使用时,只需要声明 progress_timer 对象即可,当对象离开其生命期时,会自动输出流逝时间。在同一个小程序里测量多个时间,可以加上花括号来限定其生命期即可。

注:

progress_timer 的构造函数是: progress_timer(std::ostream& os = std::cout),所以你也可以让它输出到指定的IO流里。另,它的输出精度只有小数点后两位。

 1   #include <iostream>
 2   #include <boost/progress.hpp>
 3 
 4   int main()
 5   {
 6       {
 7           boost::progress_timer t;
 8           for(int i = 0; i< 100000000;i++)
 9           {
10               ;
11           }
12       }
13       {
14           boost::progress_timer t;
15           for(int i = 0; i< 100000000;i++)
16           {
17               ;
18           }
19       }
20   }


2、boost::progress_display

作用:

动态输出任务进度。

使用:

通过构造函数传入进度基数,再在合适的地方进行前置自加,来更新进度。

注:

固有缺陷是不能把进度输出与程序输出分离,所以如果程序中也有输出操作,那么输出格式就会显得很乱。一个解决的办法是,在每次显示进度的时候,都调用 restart() 重新显示进度刻度,然后用 operator+=来指定当前进度,而不是简单的调用 operator++。

  #include <iostream>
  #include <boost/progress.hpp>

  int main()
  {
      int i(100000000),j(100000000),k(1);
      boost::progress_display pd(i);
      boost::progress_timer t;
      while(i>0)
      {
          while(j>0)
          {
              j--;
          }
          i--;
          ++pd;   //更新进度显示
      }
  }

输出:

原文地址:https://www.cnblogs.com/tianyajuanke/p/2683244.html