#includeros::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法三: ompstd::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";
基于OpenMP,没有类的封装,用起来更直观一些。(可能效率也更高点?)
#include测试1double start = omp_get_wtime(); //单位秒 ... do some work ... double end = omp_get_wtime(); //单位秒 double diff = end - start; std::cout << "Time used: " << diff << " s";
// // 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最差!
ReferenceC++ API文档
ROS与C++入门教程-Time(时间)
omp_get_wtime | Microsoft Docs



