# Function call significantly slower in service handler

I found that if I call my function within a service handler it runs significantly slower (about 10x slower). I have not notice this problem before with my other service calls. I suspect it is because this function call uses a lot of memory, but not knowing how service handlers work under the hood I have no idea how to fix this problem.

My code is copied below. Some insights will be much appreciated. I am currently using ros hydro.

class CNNStateManagerInterface:
def __init__(self, settings):
self.cnn_state_manager = CNNStateManager(settings)
s = rospy.Service('get_cnn_list_state', GetListState, self.handle_get_cnn_list_state)
# function runs normal speed here
self.cnn_state_manager.get_cnn_list_state(None)

def handle_get_cnn_list_state(self,req):
# function runs 10 times slower if placed in the handler
self.cnn_state_manager.get_cnn_list_state(None)

if __name__ == '__main__':
cnn_state_manager_i = CNNStateManagerInterface(settings)
rospy.spin()


(Update) Based on gvdhoorn's insight I moved the initialization inside the handler and the function now runs normal speed. However, the initialization takes a lot of time and I am hoping to only need to do it once. Also I am still puzzled by why it slows the whole function down uniformly instead of having an overhead of copying memory to the thread. Appreciated if anyone has a better solution for this.

class CNNStateManagerInterface:
def __init__(self, settings):

s = rospy.Service('get_cnn_list_state', GetListState, self.handle_get_cnn_list_state)

def handle_get_cnn_list_state(self,req):
self.cnn_state_manager = CNNStateManager(settings)
# function runs normal speed now
self.cnn_state_manager.get_cnn_list_state(None)

if __name__ == '__main__':
cnn_state_manager_i = CNNStateManagerInterface(settings)
rospy.spin()


(Update 2) I did some profiling with the python profiling tool yappi. The following are the time differences of a few functions that might be interesting. As you can see, the avg function time and total time between two pycaffe calls are very different.

Initialize in init:

left to right (name    num call    tsub    time total    time avg )

/pycaffe.py:72 Classifier._Net_backward_from_layer    160    0.041332    88.79009    0.554938 tcpros_base.py:650 TCPROSTransport.receive_once    13929    0.564422    14.48369    0.001043

Initialize in handler:

/pycaffe.py:72 Classifier._Net_backward_from_layer    160    0.029367    0.688773    0.004305 tcpros_base.py:650 TCPROSTransport.receive_once    4118    0.115029    1.749794    0.000378

I thought it might be due to using pycaffe functions with multi-threading so I tested threads instead of ROS handler, however the speed is normal when I run it in a thread like the following.

class CNNStateManagerInterface:
def __init__(self, settings):
self.cnn_state_manager = CNNStateManager(settings)

t.start()
def handle_get_cnn_list_state(self,req):
# function runs normal speed now
self.cnn_state_manager.get_cnn_list_state(None)

edit retag close merge delete