代码改变世界

boost 库之时间处理 (cpu_timer auto_cpu_timer)(timer,progress_timer, progress_display)

2012-12-25 23:31  Scott Guthrie Liu  阅读(357)  评论(0编辑  收藏  举报

boost目前推荐 cpu_timer  auto_cpu_timer

#include <boost/timer/timer.hpp>
View Code
#include <boost/timer/timer.hpp>
 #include <memory>
 #include <vector>
 #include <string>
 #include <iostream>
 
 using namespace std;
 using namespace boost::timer;
 
 
 vector<string> createVector_98()
 {
     vector<string> vec;
     for (int i = 0; i < 10; ++i){
             vec.emplace_back("helloworld");
     }
     return vec;
 }
 
 vector<string> createVector_11()
 {
     vector<string> vec;
     for (int i = 0; i < 100; ++i){
         vec.emplace_back("helloworld");
     }
     return move(vec);
 }
 
 int main()
 {
     const int TEST_TIMES = 100;
 
     vector<string> result;
 
     cpu_timer timer;
     timer.start();
     for (int i = 0; i < TEST_TIMES; ++i){
         result = createVector_98();
     }
     cout << "no move" << timer.format(6) << endl;
 
     timer.start(); // don't call resume()
     
     for (int i = 0; i < TEST_TIMES; ++i){
         result = createVector_11();
     }
     cout << "use move" << timer.format(6) << endl;
 }

http://www.cnblogs.com/hdtianfu/archive/2012/09/14/2684217.html

旧的库 timer <boost/timer.hpp>  <boost/progress.hpp>

View Code
#include "stdafx.h"
#include <iostream>

#include "boost/timer.hpp"
//using namespace boost;

int _tmain(int argc, _TCHAR* argv[])
{
    boost::timer t;

    std::cout << t.elapsed_max() << std::endl; //可度量的最大时间
    std::cout << t.elapsed_min() << std::endl; //可度量的最小时间,以秒为单位
    std::cout << t.elapsed() << std::endl;       //输出流逝的时间
    return 0;
}
View Code
#include "stdafx.h"

#include "boost/progress.hpp"
#include "boost/static_assert.hpp"

//精度可控制
template<int N = 2>
class new_progress_timer:public boost::timer
{
public:
    new_progress_timer(std::ostream& os = std::cout)
        : m_os(os)
    {
        BOOST_STATIC_ASSERT(N >= 0 && N <= 10);
    }

    ~new_progress_timer()
    {
        try
        {
            std::istream::fmtflags old_flags = 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_flags);
            m_os.precision(old_prec);
        }
        catch (...)
        {
        }
    }
private:
    std::ostream& m_os;
};

int _tmain(int argc, _TCHAR* argv[])
{
    new_progress_timer<10> t;
    ::Sleep(1010);
    return 0;
}
View Code
#include "boost/progress.hpp"
#include "boost/thread.hpp"
 int _tmain(int argc, _TCHAR* argv[])
 {
     //在控制台上显示程序执行进度
     boost::progress_display t(1000);
     for (int i = 0; i < 1000; i++)
     {
         boost::this_thread::sleep(boost::posix_time::milliseconds( 1010 ) );
         ++t;
     }

     return 0;
 }

http://blog.csdn.net/chollima/article/details/7536077