ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How inefficient is it to create the same publisher multiple times?

asked 2015-10-14 11:21:29 -0500

Felix Duvallet gravatar image

I'm curious about the potential performance hit of creating the same publisher multiple times, for example in a callback:

def callback(self, msg):
    publisher = rospy.Publisher(topic, String, queue_size=1)

versus starting the publisher once and storing it:

def __init__(self, topic):
    self.publisher = rospy.Publisher(topic, String, queue_size=1)
def callback(self, msg):

I have some code that will publish to (potentially) many topics; creating all the publishers at init may not be worth the additional hassle of bookkeeping if it's just as easy to just create the publishers on an as-needed basis.

For what it's worth, I ran some ipython timing code:

import rospy
from std_msgs.msg import String
%timeit p = rospy.Publisher('asdf', String, queue_size=10)
100000 loops, best of 3: 6.79 us per loop

It doesn't appear that starting the publisher is very expensive performance-wise.

Is there anything else I'm missing that would be a downside of starting the same publisher multiple times?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-10-14 14:22:58 -0500

With your first example, using the publisher immediately after it is created will almost certainly result in the published message not actually being published. It takes a nonzero amount of time for the publisher to register itself with the master, but is a non-blocking call.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-10-14 11:21:29 -0500

Seen: 181 times

Last updated: Oct 14 '15