Ask Your Question
0

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

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

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)
    self.publisher.publish(msg.data)

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):
    self.publisher.publish(msg.data)

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
2

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 101 times

Last updated: Oct 14 '15