• C++ 常用时间获取函数汇总


    法一: ros

    #include <ros/ros.h>
    ros::Time begin = ros::Time::now();
    ... do some work ...
    ros::Time end = ros::Time::now();
    ros::Duration diff = begin - end;
    double sec = diff.toSec();
    std::cout << "Time used:  " << sec << " s";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    另外ros还有睡眠和指定频率循环的功能:

    ros::Duration(0.5).sleep(); // sleep for half a second
    
    /// 以10Hz的频率执行以下循环
    ros::Rate r(10); // 10 hz
    while (ros::ok())
    {
      ... do some work ...
      r.sleep();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    还可以使用 ros::Timerros::Rate更灵活。具体参见:https://www.ncnynl.com/archives/201701/1284.html

    法二: chrono

    #include <chrono>
    std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
    ... do some work ...
    std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    std::chrono::duration<double> diff = end - start;
    std::cout << "Time used:  " << diff.count() << " s";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    法三: omp

    基于OpenMP,没有类的封装,用起来更直观一些。(可能效率也更高点?)

    #include <omp.h>
    double start = omp_get_wtime(); //单位秒
    ... do some work ...
    double end = omp_get_wtime(); //单位秒
    double diff = end - start;
    std::cout << "Time used:  " << diff << " s";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    测试1

    //
    // Created by daybeha on 2022/6/27.
    //
    
    #include <iostream>
    #include <chrono>
    #include <ros/ros.h>
    #include <omp.h>
    
    using namespace std;
    
    int main(int argc, char** argv){
        ros::init(argc,argv, "time_acquire");
        ros::NodeHandle n;
        ros::Rate r(50000);
    
        // 法一
        cout << "ros:" << endl;
        ros::Time begin_r = ros::Time::now();
        std::cout << "Start  " << begin_r.toSec() << " s"<< endl;
        r.sleep();
        ros::Time end_r = ros::Time::now();
        std::cout << "End  " << end_r.toSec() << " s"<< endl;
        ros::Duration diff_r = end_r - begin_r;
        double sec = diff_r.toSec();
        std::cout << "Time used:  " << sec << " s" << endl;
    
    
        // 法二
        cout << endl << "chrono:" << endl;
        std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
        std::cout << "Start  " << start.time_since_epoch().count() << " s"<< endl;
        r.sleep();
        std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
        std::cout << "End  " << end.time_since_epoch().count() << " s"<< endl;
        std::chrono::duration<double> diff = end - start;
        std::cout << "Time used:  " << diff.count() << " s"<< endl;
    
        // 法三
        cout << endl << "OpenMP:" << endl;
        double start_o = omp_get_wtime(); //单位秒
        std::cout << "Start  " << start_o << " s"<< endl;
        r.sleep();
        double end_o = omp_get_wtime(); //单位秒
        std::cout << "End  " << end_o << " s"<< endl;
        double diff_o = end_o - start_o;
        std::cout << "Time used:  " << diff_o << " s"<< endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    rate=100Hz
    在这里插入图片描述

    rate=1000Hz
    在这里插入图片描述

    rate=10000Hz
    在这里插入图片描述

    rate=50000Hz
    在这里插入图片描述
    rate=100000Hz
    在这里插入图片描述

    可以看到,随着频率增大,三种方法的时间都不怎么准了(当然r.sleep应该也不准了……),其中chrono最差。 ros整体耗时更高。

    测试2

    //
    // Created by daybeha on 2022/6/27.
    //
    
    #include <iostream>
    #include <chrono>
    #include <ros/ros.h>
    #include <omp.h>
    
    using namespace std;
    
    void test(){
        for (int i = 0; i < 10000; ++i) {
            for (int j = 0; j < 1000; ++j) {
                int a = i*j;
            }
        }
    }
    
    int main(int argc, char** argv){
        ros::init(argc,argv, "time_acquire");
        ros::NodeHandle n;
        ros::Rate r(100000);
    
        // 法一
        cout << "ros:" << endl;
        ros::Time begin_r = ros::Time::now();
        std::cout << "Start  " << begin_r.toSec() << " s"<< endl;
    //    r.sleep();
        test();
        ros::Time end_r = ros::Time::now();
        std::cout << "End  " << end_r.toSec() << " s"<< endl;
        ros::Duration diff_r = end_r - begin_r;
        double sec = diff_r.toSec();
        std::cout << "Time used:  " << sec << " s" << endl;
    
    
        // 法二
        cout << endl << "chrono:" << endl;
        std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
        std::cout << "Start  " << start.time_since_epoch().count() << " s"<< endl;
    //    r.sleep();
        test();
        std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
        std::cout << "End  " << end.time_since_epoch().count() << " s"<< endl;
        std::chrono::duration<double> diff = end - start;
        std::cout << "Time used:  " << diff.count() << " s"<< endl;
    
        // 法三
        cout << endl << "OpenMP:" << endl;
        double start_o = omp_get_wtime(); //单位秒
        std::cout << "Start  " << start_o << " s"<< endl;
    //    r.sleep();
        test();
        double end_o = omp_get_wtime(); //单位秒
        std::cout << "End  " << end_o << " s"<< endl;
        double diff_o = end_o - start_o;
        std::cout << "Time used:  " << diff_o << " s"<< endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    在这里插入图片描述

    结论: chrono获取时间的效率最高,OpenMP其次,ros最差!

    Reference

    C++ API文档
    ROS与C++入门教程-Time(时间)
    omp_get_wtime | Microsoft Docs

  • 相关阅读:
    【23-24 秋学期】NNDL 作业8 卷积 导数 反向传播
    MySql学习笔记01——SQL的相关术语
    selenium上传文件时打开指定本地文件路径
    创建并配置一个新的python环境
    计算机网络4小时速成:计算机网络基础,计网组成,计网分类,性能指标,标准化组织,计网结构模型,五层模型
    C++面试八股文:用过std::set/std::map吗?
    flink实战--flinksql 窗口TVF
    googleTest V1.12.1的基础用法
    Java Stack 类
    iOS关于高德地图定位和热点搜索使用小结
  • 原文地址:https://blog.csdn.net/weixin_48592526/article/details/125418059