Ask Your Question
0

Same Algorithm works very slow in windows and very fast in Ubuntu.

asked 2014-07-08 03:06:13 -0600

chris_chris gravatar image

updated 2014-07-08 06:57:38 -0600

I have a same algorithm working in two computer. One with Ubuntu and other with windows.

In Ubuntu, the algorithm takes a input images through a framework called ADTF. Here the algorithm works in 70fps. Everything is perfect.

In windows, the algorithm takes input images from a video stream without using any framework (visual studio 2012 as an IDE). Here the algorithm works in 2fps.

When I checked for the computation time, I see a function in my algorithm which takes 0.4seconds in windows and 0.011seconds in Ubuntu. And this function has been called 'n' number of times in the process. This function uses only cv::Mat in its argument and some float. I don't think so the data type float is responsible for this big time variation.

I checked the every single line of the c++ codes in my files. Its exactly same in both cases. But only the computation time has been decreased more than 30 times. I have no idea what's happening. Can anyone help me?

Note:: Both the computer have exactly the same configuration.

Intensity getIntensity (Grids &inputGrids, Mat inputImage)// groups of 2d grids {
    type allGridIntensity;
    for()//takes single grid
    {
        type singleGridIntensity;
        for()//takes single point
        {
            double intensity = inputImage.at<uchar>(yPose, xPose);
            singleGridIntensity.push_back(intensity);
        }
        allGridIntensity.push_back(singleGridIntensity);
    }
    return allGridIntensity; }
edit retag flag offensive close merge delete

Comments

1

First of all, it is impossible to have exactly the same configuration ... there can be differences in compiler settings. Secondly, did you build both openCV configurations with TBB support? Thirdly, without any idea of what algorithm is going slow or what function is working slower, we cannot help you, so more info is required. Can you provide this? Also are you sure you aren't mixing up debug and release configurations? These make a HUGE difference. Fourthly, you are using two different capturing techniques. Can you take a universal capturing technique for comparing that works on both OS?

StevenPuttemans gravatar imageStevenPuttemans ( 2014-07-08 03:18:05 -0600 )edit
1

you probably want to show the offending piece of code

berak gravatar imageberak ( 2014-07-08 03:18:27 -0600 )edit

@StevenPuttemans. I meant the hardware configuration(just 2 laptops with same model).. I dont know what is TBB support (so I ll answer this later). I have uploaded the function which consumes time (see updated edit). No, I am not mixing up the debug and release. I using only the release configs.

chris_chris gravatar imagechris_chris ( 2014-07-08 07:03:34 -0600 )edit

@berak and @StevenPuttemans I have uploaded the piece of a code. This takes a group of grid and a cv::Mat as argument. Each grid is defined by group of points. I defined this point with type float. This function just takes the instensity value for each point (x, y) in all the grid.

chris_chris gravatar imagechris_chris ( 2014-07-08 07:07:31 -0600 )edit

@StevenPuttemans. In my windows PC, I use it with TBB support. And no idea about Ubutu PC. Because I cannot use the Ubuntu PC anymore. (And that is the reason for this question)

chris_chris gravatar imagechris_chris ( 2014-07-08 07:59:28 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-07-08 07:39:27 -0600

updated 2014-07-08 08:04:15 -0600

Look like you try to get intensity values of divided sub-images, in order, from a bigger one and store them in a Mat or a vector of double? IMO, real code makes it is easier to get real answers. However, my answer is: you do not need an extra statement before the push_back call of singleGridIntensity, just performing a direct emplace_back:

singleGridIntensity.emplace_back(double(inputImage.at<uchar>(yPose, xPose)));

Indeed, the trick here is emplace_back function (a C++ 11 one). In my experience, it often gives about 20% improvement in speed, especially with large inputs and a huge number of calls. If the numbers of intensity values of grids are the same, you can declare a local vector and do a direct assignment:

singleGridIntensity[i++]=double(inputImage.at<uchar>(yPose, xPose));

Then, with the allGridIntensity variable: change push_back to emplace_back and, if you run it thousands of times, do not return a Mat or a vector, just pass a reference of that type in the parameter list and change its content as your expected return values, this will offer another enhancement in speed. Finally, the code will be something like this:

void getIntensity (Grids &inputGrids, Mat inputImage, vector<type> & allGridIntensity)
{
    for()//takes single grid
    {
        vector<type> singleGridIntensity[N];
        for(i=0;i<N;)//takes single point
            singleGridIntensity[i++] = type(inputImage.at<uchar>(yPose, xPose)); // a type cast

        allGridIntensity.emplace_back(singleGridIntensity);
    }
}

Also, try to access each pixel by pointer (like from this link), you may gain some more improvements.

edit flag offensive delete link more

Comments

Thank you for the answer. really very good answer for improvising the speed. But especially, I like know why my algo is nearly 30 times faster in Ubuntu.

chris_chris gravatar imagechris_chris ( 2014-07-08 07:52:41 -0600 )edit

I will try this out soon and add my results. And I am sure this will increase the speed of my algo. If implement this in both of my cases. Again Ubuntu is going to much faster than Windows and nearly the same. I asking this because no more I cannot use the system with Ubuntu.

chris_chris gravatar imagechris_chris ( 2014-07-08 07:55:06 -0600 )edit
1

In fact, with the same code, on the same machine and OS (Windows), the speed of a program compiled by GCC and that of the one generated by VS 2012 (may be it will be better with VS 2013) are not the same, particularly with vector's operations. When optimization options are tuned on, I found GCC faster than VS 2012. But 30x faster as your results is strange to me. May be you should try your code on the Windows OS, with GCC and VS to see the difference.

tuannhtn gravatar imagetuannhtn ( 2014-07-08 08:00:31 -0600 )edit

I think in my case this is the problem. Is there any alternatives in VS 2012 (instead of optimization options in gcc)?

chris_chris gravatar imagechris_chris ( 2014-07-09 02:34:56 -0600 )edit

I have no idea at the moment, but a suggestion: why don't you post your real code, your hardware and software platform details (which version of OpenCV do you use, ...), your test data (image size, grid size, number of iterations) and results. With more detailed information, other OpenCV gurus of this site may give you better solutions.

tuannhtn gravatar imagetuannhtn ( 2014-07-09 02:50:31 -0600 )edit

Question Tools

Stats

Asked: 2014-07-08 03:06:13 -0600

Seen: 3,207 times

Last updated: Jul 08 '14