码迷,mamicode.com
首页 > 其他好文 > 详细

标准库时间工具

时间:2020-07-18 13:45:36      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:poi   pos   std   min   class   end   ace   time   div   

//关于duration: 
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;

void f(int x)
{
    for(int i=0;i!=x;++i)
        cout << i <<  ;
    cout << endl;
}

int main()
{
    auto t1 = system_clock::now();
    f(10); // do something
    auto t2 = system_clock::now();
    
    auto dms = duration_cast<milliseconds>(t2-t1);
    cout << "f(x) took " << dms.count() << " milliseconds\n";
    auto ds = duration_cast<seconds>(t2-t1);
    cout << "f(x) took " << ds.count() << " seconds\n";
    
    return 0;
}
--------------------------------------------------------------------------------------------
//关于time_point: 
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;

int main()
{
    auto tp = steady_clock::now();
    auto d1 = time_point_cast<hours>(tp).time_since_epoch().count()/24;   //从纪元开始到现在的天数
    
    using days=duration<long,ratio<24*60*60,1>>;
    auto d2 = time_point_cast<days>(tp).time_since_epoch().count();
    
    if(d1!=d2) cout << "Impossible!\n";
    return 0;
}
-----------------------------------------------------------------------------------------------
//关于clock: 
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;

int main()
{
    cout << "min " << system_clock::duration::min().count()
           << ", max " << system_clock::duration::max().count()
         << ", " << (treat_as_floating_point<system_clock::duration>::value?"FP":"integral") << \n;
    
    cout << (system_clock::is_steady?"steady\n":"not steady\n");
    
    return 0;
}

 

标准库时间工具

标签:poi   pos   std   min   class   end   ace   time   div   

原文地址:https://www.cnblogs.com/lhb666aboluo/p/13335279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!