ROS 2 runs obviously less efficient with custom message than with build-in message

7 views (last 30 days)
Hello,
I use currently ROS 2 to let agents communicate with each other. I successfully created a custom message using MATLAB function ros2genmsg() to meet my need. However, I noticed that when I used custom message, MATLAB ran significantly slower than I used build-in message. I also tested it with the custom message given in the MATLAB doc example.
Results: the execution of ros2message() with custom message type needs about 0.16 sec, while the time is less than 0.0005 sec with build-in message. The same phenomenon for ros2publisher() and ros2subscriber().
Reproduce:
  1. run openExample('ros/ROS2CustomMessagesExample') in MATLAB command window
  2. run the opened live skript, where a custom message called "example_b_msgs/Standalone" will be created.
  3. run the following test code:
build_in_msg = "std_msgs/ByteMultiArray"; % build-in message
custom_msg = "example_b_msgs/Standalone"; % custom message
tic
for i=1:10
ros2message(build_in_msg);
end
toc % -> Elapsed time is 0.005391 seconds.
tic
for i=1:10
ros2message(custom_msg);
end
toc % -> Elapsed time is 1.627962 seconds.
There are some ways to acoid this problem. Simplest one is that I use ros2message() only once and modify the created variable whenever I need but not call the function again.
I'm wondering if there is something I can do to let ROS 2 custom message run as efficient as build-in message.
Any help would be appreciated.
(Note: I am using MATLAB R2022a in Windows 10 with version 21H2.)
Best,
Jianye Xu

Accepted Answer

Jagadeesh Konakalla
Jagadeesh Konakalla on 3 Jun 2022
Hi Jianye Xu,
I am able to reproduce the issue that you reported.
The way custom messages are registered with MATLAB are causing this issue. By deisgn, we assumed that this function can be used only once in a script or function and not in the loop.
For now, as you mentioned, the solution i advice you to use ros2message() only once and not the call the function in loop.
Thanks,
Jagadeesh K.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by