• c++11 Chrono时间库


    c++11 Chrono时间库

    http://en.cppreference.com/mwiki/index.php?title=Special%3ASearch&search=chrono


    1. 持续时间 duration

    template<class Rep, class Period = ratio<1>>
    class duration;
    

    class Rep: 滴答数
    class Period: 滴答周期,默认1秒

    1) 常用方法

    count / zero / min / max
    支持算术运算 + - * / % ++ -- += -= *= /= %= (不同的Period之间会自动转换)

    2) 示例

    duration<long, ratio<60>> d1(2);  //2分钟
    duration<long, ratio<1>>  d2(30); //30秒
    duration<double, ratio<60>> d3 = d1+d2; //2.5分
    count << d3.count() << "minutes" << endl;
    

    3) 标准定义

    //X 64 bits表示:有符号整数类型,且位数至少为64位
    typedef duration<X 64 bits, nano>  nanoseconds; //纳秒
    typedef duration<X 55 bits, micro> microseconds; //微秒
    typedef duration<X 45 bits, milli> milliseconds; //毫秒 
    typedef duration<X 35 bits> 	   seconds; //秒 
    typedef duration<X 29 bits, ratio<60>>    minutes; //分
    typedef duration<X 23 bits, ratio<3600>>  hours; //时
    
    //例
    auto t = hours(1) + minutes(2) + seconds(45);
    count << seconds(t).count() << endl;
    

    2. 时钟 clock

    • system_clock 系统实时时钟的真实时间
    • steady_clock time_point绝不递减的时钟
    • high_resolution_clock 高精度时钟

    1) 常用接口

    // 均为静态接口
    now()  //获取类型为time_point的当前时间点
    to_time_t()  //将time_point转换为time_t类型
    from_time_t()  //将time_t转换为time_point类型
    

    localtime可将time_t类型转换为tm表示的本地时间

    2) 示例

    //获取当前时间
    #include <time.h>      // time_t, locaktime, tm
    #include <iomanip>     // put_time
    system_clock::time_point tpoint = system_clock::now();
    time_t tt = system_clock::to_time_t(tpoint);
    tm* t = locaktime(&tt);
    cout << put_time(t, "%H:%M:%S") << endl;
    
    //计算代码的执行时间
    auto start = system_clock::now();
    // ... to do something
    auto end = system_clock::now();
    auto diff = end - start;
    cout << duration<double, milli>(diff).count() << "ms" << endl;
    

    3. 时点 time_point

    • time_point是一个时间点,存储相对于纪元(1970/01/01)的一个duration。
    • time_since_epoch() 返回当前时间点到纪元的duration。
    • 每一个time_point都关联一个clock,创建时需指定clock作为模板参数。

    1) 构造函数

    time_point(); 			//通过duration::zero初始化表示关联clock的纪元
    time_point(const duration& d); //通过duration初始化表示纪元+d
    template<class Duration2>
    time_point(const time_point<clock, Duration2>& t); //通过t.time_since_epoch初始化
    

    2) 示例

    time_point<steady_clock> tp1;
    tp1 += minutes(10);
    auto d1 = tp1.time_since_epoch();
    duration<double> d2(d1);
    cout << d2.count() << "seconds" << endl;
    

  • 相关阅读:
    分页查询
    matlab 常用机器学习算法的实现
    powerpoint(ppt) 的制作
    powerpoint(ppt) 的制作
    libsvm 的使用
    libsvm 的使用
    TFRecord —— tensorflow 下的统一数据存储格式
    numpy 数据类型与 Python 原生数据类型
    numpy 数据类型与 Python 原生数据类型
    Python str 与 bytes 类型(Python2/3 对 str 的处理)
  • 原文地址:https://www.cnblogs.com/songcf/p/4575378.html
Copyright © 2020-2023  润新知