当前位置:网站首页>获取一段程序运行的时间
获取一段程序运行的时间
2022-08-09 18:49:00 【可即】
查看一段ROS代码的运行时间
在ROS的运行过程中,始终存在一个时间戳,这个时间戳可以用来查看代码的运行时间:
ros::Time start = ros::Time::now();
cout << start << endl;
代码串
ros::Time end = ros::Time::now();
cout << end << endl;
添加两个时间戳打印结果,单位是秒,通过两个时间戳相减可以得到这段程序的运行时间。
查看一段C/C++代码的运行时间
1)用clock()函数
#include<iostream>
#include<time.h>
using namespace std;
int main()
{
clock_t startTime,endTime;
startTime = clock();//计时开始
for ( int i = 0; i < 2147483640; i++)
{
i++;
}
endTime = clock();//计时结束
cout << "The run time is:" <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
cout << "The run time is:" << (double)clock() /CLOCKS_PER_SEC<< "s" << endl;
system("pause");
return 0;
}
输出:
The run time is:3.67578s
The run time is:3.67688s
clock函数返回进程运行时间,但是这个运行时间单位不是秒,而是CPU运行的时钟周期计数。
所以要得到消耗的时间(秒),需要除以CPU时钟频率,也就是CLOCKS_PER_SEC.以得到一个以秒为单位的数值。
printf(“%.2lf\n”,(double)clock()/CLOCKS_PER_SEC);因此以上可输出程序运行时间。
2)使用std::chrono
#include<iostream>
#include <chrono>
using namespace std;
int main()
{
chrono::system_clock::time_point t1 = chrono::system_clock::now();
//需测试运行时间的代码
for ( int i = 0; i < 2147483640; i++)
{
i++;
}
chrono::system_clock::time_point t2 = chrono::system_clock::now();
chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
cout << "run time = " << time_used.count() << " seconds. " << endl;
return 0;
}
c++11的时间库chrono均位于名字空间std::chrono下,std::chrono时间的更多用法参考https://blog.csdn.net/sunlin972913894/article/details/103194920
3)使用Boost库中的timer
#include<iostream>
#include<boost/timer.hpp>
using namespace std;
int main()
{
boost::timer t;
//需测试运行时间的代码
for ( int i = 0; i < 2147483640; i++)
{
i++;
}
cout<<"运行时间:"<<t.elapsed() <<"s"<<endl;//输出已流失的时间
return 0;
}
timer类可以测量时间的流逝,是小型计时器,提供毫秒级别的计时精度。
还有其他方式获取程序运行时间,如timeGetTime()函数,暂且提供以上几种方式
边栏推荐
- 华为云全流程护航《流浪方舟》破竹首发,打造口碑爆款
- pat链表专题训练+搜索专题
- 2022.08.08_每日一题
- shell脚本编写 hash方法,shell中字符到ascii码或数字的转换
- 时序攻击
- 新出现的去中心化科学能够为科学领域带来什么?
- hdu 2094 产生冠军(STL map || 拓扑 || STL set)
- Paper sharing: "FED BN" uses the LOCAL BATCH NORMALIZATION method to solve the Non-iid problem
- [] free column Android dynamic debugging GDB APP of safety
- laravel之phpunit单元测试
猜你喜欢
Tims中国上市进入倒计时:年亏3.8亿 估值降至14亿美元
毕昇编译器优化:Lazy Code Motion
基于SSM实现手机销售商城系统
2022深圳(软考中级)系统集成项目管理工程师报名
[] free column Android run Android, her - as command of safety
【IoT毕设】STM32与机智云自助开发平台的宠物智能喂养系统
『百日百题 · 基础篇』备战面试,坚持刷题 第五话——循环语句(2)!
嵌入式开发:使用FILL提高代码完整性
Haven't tried line art videos this year??
【kali-权限提升】(4.2.6)社会工程学工具包(中):中间人攻击工具Ettercap
随机推荐
小满nestjs(第三章 前置知识装饰器)
【分享】入驻集简云开发者平台,如何使用Session Auth配置授权?
2022.08.05_每日一题
[免费专栏] Android安全之和平精英(FZ)APK逆向分析
OpenSSL SSL_read: Connection was reset, errno 10054
laravel 时区问题timezone
日期及时间处理包 Carbon 在 Laravel 中的简单使用[通俗易懂]
Fully automated machine learning modeling!The effect hangs the primary alchemist!
ClickHouse一种高性能分布式join查询模型(Colocate Join)
laravel之phpunit单元测试
AttributeError: module 'click' has no attribute 'get_os_args'
IDEA tools commonly used configuration
axi4c
2022深圳(软考高级)信息系统项目管理师认证报名
leetcode 503.下一个更大元素II 单调栈
这年头还不来尝试线稿图视频??
典型的数据仓库模型实施过程详解
pytest框架之mark标记功能详细介绍
mysql duplicate data group multiple latest records
IS31FL3737B 通用12×12 LED驱动器 I2C 42mA 40QFN