ROS2机器人-C++和Python怎么选

本文探讨了C++与Python在ROS2环境下节点应用的性能差异,特别是在CPU使用率方面,发现Python节点的CPU使用率普遍高于C++节点数倍,并讨论了可能的原因及应对策略。
部署运行你感兴趣的模型镜像

发布选择:C++,开发选择:Python。

由于工作性质,教学过程中适当使用Python演示是可行的,但是一直致力于推介C++。

使用C++最大问题,学生C++基础比较薄弱。


discourse.ros.org/t/high-cpu-load-for-simple-python-nodes/28324


Marco:
I have been using in a project various nodes, some of which in C++, and some in Python. I noticed a huge difference, even for basic nodes that just do some publishing, in CPU usage between Python and C++. Python CPU usage is generally several times higher than a C++ node doing the same.
This happens for publisher nodes, as well as for server nodes, even in their idle state when no services or actions are actually requested, which I find extremely strange…

Did anyone notice similar issues? Does anyone have any recommendations for reducing the CPU usage of Python nodes?
The issue has been observed so far in ROS2

马可:
我在一个项目中使用了各种节点,其中一些在C++中,一些在Python中。我注意到Python和C++之间的CPU使用情况存在巨大差异,即使对于只做一些发布的基本节点也是如此。Python的CPU使用率通常比C++节点高几倍。
这种情况发生在发布者节点和服务器节点上,即使在它们的空闲状态下,实际上没有请求任何服务或操作,我觉得这非常奇怪…
有人注意到类似的问题吗?有人对减少Python节点的CPU使用有什么建议吗?
到目前为止,在不同的CPU架构(amd和arm)下,在ROS2银河系统中已经观察到了这个问题


aposhian:
I have observed this as well. For this reason, I only use rclpy for development, and never for production nodes.

阿波西安语:
我也观察到了这一点。因此,我只将rcpy用于开发,而从不用于生产节点。


matthews-jca:
Not just CPU overhead when spinning, but memory footprint as well. Mirroring Aposhian we usually prototype in python and then convert to C++ for release

马修斯jca:
不仅是旋转时的CPU开销,还包括内存占用。镜像Aposhian我们通常在python中创建原型,然后转换为C++发布


MoffKalast:
At the risk of sounding callous, you guys do realize you’re comparing a compiled language known for its memory efficiency and speed to one that’s none of those three things. The virtual machine will add overhead, so will the interpreter, and of course the language itself needs more processor cycles to run, given dynamic typing and other high level abstractions. Even if you’re not receiving anything there’s still a thread in the background that needs to periodically poll for new socket data I presume. Unlike C++ which will strip out all unused code at compile time, Python also needs to keep all imported modules loaded into memory.

But none of us are using Python because it’s fast or memory efficient, we’re using it because it cuts down development time by an order of magnitude 

莫夫卡拉斯特:
冒着听起来冷酷无情的风险,你们确实意识到,你们正在将一种以内存效率和速度著称的编译语言与这三种语言相比。虚拟机会增加开销,解释器也会增加开销。当然,考虑到动态类型和其他高级抽象,语言本身需要更多的处理器周期来运行。即使您没有收到任何消息,我想后台仍有一个线程需要定期轮询新的套接字数据。与C++不同,Python在编译时会删除所有未使用的代码,Python还需要将所有导入的模块加载到内存中。
但我们没有人使用Python是因为它速度快或内存效率高,我们使用它是因为它将开发时间缩短了一个数量级


您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论 8
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangrelay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值