How inefficient is it to create the same publisher multiple times?
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?