Rosanswers logo

I had a dilemma whether to create a custom message that has all the variables of a certain node or to convert those variables to a dictionary and send it as a string on a ROS topic.
So my question is which way is better when we compare it with data transmission. Which one will be faster and consume less memory?
Thanks in advance.

Originally posted by Flash on ROS Answers with karma: 126 on 2022-07-21

Post score: 0


1 Answer 1


Rosanswers logo

So my question is which way is better when we compare it with data transmission.

Using semantically appropriate messages is always the "better" way.

Is your data structure a string? At the very least it's a dictionary, as you describe yourself. A dictionary != string.

Do not use strings to transmit data structures.

It's unnecessary and undesirable.

Which one will be faster and consume less memory?

Unless you have evidence already using a custom message (with proper fields) is causing you performance issues, do not prematurely optimise.

Originally posted by gvdhoorn with karma: 86574 on 2022-07-24

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by Flash on 2022-07-24:
I was thinking that since using a custom message will have multiple fields means each field will have a key and values kind of data structure. This is similar to a dictionary data structure.
So are there any advantages of using a custom message over converting a dictionary to a string and sending it as a string, because both ways it will have the same amount of data as per my understanding? or when we use ROS custom messages it only transmits values and decodes those values at that node using a custom data structure format.

Comment by gvdhoorn on 2022-07-25:
Only values are transmitted.

But that's not the point here.

A string has no meaning. If you receive a std_msgs/String, could you tell me what it contains? Other than some characters?

If you receive (for example) a sensor_msgs/PointCloud2, could you tell me what it contains? I believe you can: a pointcloud.

So semantically, it's clear that a sensor_msgs/PointCloud2 is not a string.

That's what the guiding principle should be when using ROS: clear, semantically meaningful messages.

Comment by Flash on 2022-07-25:\

Only values are transmitted.

Thx that's what I was looking for.
And I do agree that the topic should have a clear indication as you mentioned.
And if it uses less memory in doing so then we should definitely go for it, if your topic is for internal communication only. Correct me if I am wrong.

Comment by BlakeAnderson on 2022-07-25:
It's unclear to me what "internal communication" means or why one should eschew good semantic practice in them.

Comment by Flash on 2022-07-25:
internal communication in terms of when you are not sharing those topics publicly


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.