Function execution is slow in callbacks. Why?
Hi My (Heavy) Image processing functionis slow in the subscriber callback. But if I run the same function outside ROS in release mode it is fast. (10X fast)
Why I want to put the image processing function in callback? Because I want to process received image as soon it arrives.
Any comments! My code looks like:
class storedData {
public:
cv::Mat inIm = cv::Mat::zeros(376, 672, CV_8UC3);
cv::Mat outIm = cv::Mat::zeros(376, 672, CV_8UC3);
void depthCallback(const sensor_msgs::CompressedImageConstPtr& msgDepth)
{
try
{
tic2 = ros::Time::now();
imProcess(inIm, outIm); // Heavy Image Processing
imPTime = ros::Time::now().toSec() - tic2.toSec();
std::cout<<"Im process time= "<<imPTime<<std::endl;
cv::imshow("view", outIm);
cv::waitKey(1);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("Could not convert the depth!");
}
}
};
int main(int argc, char **argv)
{
ros::init(argc, argv, "PredictiveDisplay");
ros::NodeHandle nh;
storedData obj;
cv::namedWindow("view", 0);
cv::startWindowThread();
ros::Subscriber subDepth = nh.subscribe("/depth/depth_registered/compressed", 2, &storedData::depthCallback, &obj);
ros::spin();
cv::destroyWindow("view");
}
If you could add your instrumentation to the example it would be helpful with an example of it running fast inside the callback but slow outside the callback we'd be able to help you. Without being able to reproduce it all we can do is guess at things.
First thing I would try would be setting ROS_BUILD_TYPE to
Release
in your CMakeLists.txt. Also clean build the package. See the linked question for options.You can read more about it here: http://wiki.ros.org/rosbuild
answers.ros.org question regarding a similar problem: catkin-compiled-code-runs-3x-slower
@gvdhoorn Thanks for your comment.
What does simple
catkin_make
do? It builds in debug or release mode?It's not Catkin that decides this, it's CMake.
If you don't configure any
CMAKE_BUILD_TYPE
, CMake will useNone
, which sets no flags at all.Thanks @gvdhoorn.
magically worked for me. I can accept this as an answer, if you can convert it to answer.
converted to an answer. Yes, it's a little surprising, the default build has no optimizations, but actually also does not have debug info either. It's really the default that you never really want.