`

Boost 学习笔记 第一天

    博客分类:
  • c++
阅读更多

 

1. timer.hpp

   timer接口简单,轻巧好用  不适合高精度,跨度大的  如以天,月,年为跨度时间单位  可以使用 date_time

     计时用的是标准头文件 <ctime>里的 std::clock()函数   精度依赖操作系统或编译器 难以跨平台

2. progress_timer.hpp 继承 timer

   省去 elapsed(),用于自动计时 小工具

   progress_timer t;//声明对象就开始计时   自动在作用域结束释放,并自动输出计时时间

 

   题外话1:

     java

            int j=0;

            for(int i=0;i<1000;i++){

              j = j++;

            }//最终 j 还是等于 0 

     c++

            int j=0;

            for(int i=0;i<1000;i++){

              j = j++;

            }//最终 j 等于 1000

     题外话2:

        oracle 函数应用 截取字符串  hello 截取掉 h

              除了用 substr ,还可以用 trim  ;

        select trim('h' from 'hello') from dual;               ello

       SELECT LTRIM('WWhhhhhaT is tHis w W','Wh') FROM DUAL;   aT is tHis w W

       SELECT RTRIM('WWhhhhhaT is tHis w W','W w') FROM DUAL;  WWhhhhhaT is tHis

       参考 http://guanhuaing.iteye.com/blog/1498792 

 

3. 扩展 progress_timer 

   progress_timer 使用方便,但是精度才小数点后两位,有些应用不能满足

   可以直接修改 progress_timer.hpp std::streamsize old_prec = m_os.precision( 2 );//2 就是精度 

   本着开一闭原则,没有预留发挥空间。 模板技术仿造 progress_timer 新类

   new_progress_timer.hpp

 

    #include <boost/progress.hpp>

   #include <boost/static_assert.hpp>

   //使用模板参数实现 progress_timer
   template<int N = 2>
   class new_progress_timer : public boost::timer
  {
    public:
        //初始化 输出流 m_os ,并用 static_assert 静态断言 保证 N取值 0和10之间
        new_progress_timer(std::ostream & os=std::cout):m_os(os){
            BOOST_STATIC_ASSERT(N>=0 && N<=10);//静态断言
        }
        ~new_progress_timer(void){
            try{
                //保存流的状态
                std::istream::fmtflags old_flages
                = m_os.setf(std::istream::fixed,std::istream::floatfield);
                std::streamsize old_prec = m_os.precision(N);

                //输出时间
                m_os<<elapsed()<<" s\n"<<std::endl;

                //恢复流状态
                m_os.flags(old_flages);
                m_os.precision(old_prec);
            }catch(...){}
        }
    private:
        std::ostream & m_os;
   };

   //使用模板特化,精度为2的直接继承自progress_timer
   template<>
   class new_progress_timer<2>:public boost::progress_timer{};
#include <stdio.h>
#include <boost/timer.hpp>
#include <boost/progress.hpp>
#include "new_progress_timer.hpp"
using namespace boost;


int main()
{
    //1. timer
    timer t;
    printf("%f h\n",t.elapsed_max()/3600);
    printf("%f s\n",t.elapsed_min());
    printf("%f s\n",t.elapsed());

    printf("\n");
    //2. progress_timer 继承 timer
    progress_timer pt;
    int j=0;
    for(int i=0;i<1000;i++){
        j = j++;
    }
    printf("%d \n",j);

    //3. 扩展progress_timer 精度为10位
    new_progress_timer<10> nt;
    for(int i=0;i<10000000;i++){
        j = j++;
    }
    printf("%d \n",j);



}

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics