栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

法一: ros
#include 
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";

另外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();
}

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

法二: chrono
#include 
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 diff = end - start;
std::cout << "Time used:  " << diff.count() << " s";
法三: omp

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

#include 
double start = omp_get_wtime(); //单位秒
... do some work ...
double end = omp_get_wtime(); //单位秒
double diff = end - start;
std::cout << "Time used:  " << diff << " s";
测试1
//
// Created by daybeha on 2022/6/27.
//

#include 
#include 
#include 
#include 

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 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;
}

rate=100Hz

rate=1000Hz

rate=10000Hz

rate=50000Hz

rate=100000Hz

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

测试2
//
// Created by daybeha on 2022/6/27.
//

#include 
#include 
#include 
#include 

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 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;
}

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

Reference

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993659.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号