Ask Your Question
0

Efficient matrix operator

asked 2014-03-28 12:11:48 -0500

wuling gravatar image

updated 2014-03-28 12:24:15 -0500

Hi all, I use mat format to calculate an matrix, also i use traditions C++ calculate an matix. But I find out, mat format is very slowly.Could someone tell me reason ? And how to improve the efficiency. Any help is highly appreciated! Thanks:) Here is my code:

  1. traditions method where sw=src2.step,it almost costs 5 number of ticks/one cycle.

     for (int k=0;k<100;k++)
     {
        int A = getTickCount();  
        for (int iwr = 0; iwr <= 11; iwr++ )
            {
              /* Sub-Window 0 */
              for (int iwc =0; iwc <= 11; iwc++ )
                {
                 gray_val =src2.data[iwr*sw+iwc];
                 sum[0] += gray_val;
                 sum_sq[0] += gray_val * gray_val;
                 }
    
    
              /* Sub-Window 1 */
           for (int iwc = 0; iwc &lt;=11; iwc++ )
            {
             gray_val = src2.data[iwr*sw+iwc];
             sum[1] += gray_val;
             sum_sq[1] += gray_val * gray_val;
             }
            }
          int B = getTickCount(); 
          int c=B-A;
    
    }

2.it almost costs 377 number of ticks/one cycle.

for(int k=0;k<100;k++)
{
            int A = getTickCount(); 
            Mat ROIimage1(src2, Rect(0,0,11,11) );
            sum1=cv::sum(ROIimage1);
            cv::multiply(ROIimage1,ROIimage1,square1);
            sum_sq1=cv::sum(square1);
        Mat ROIimage2(src2, Rect(0,0,11,11) );//=src2(ROI2);
        sum2=cv::sum(ROIimage2);
        cv::multiply(ROIimage2,ROIimage2,square2);
        sum_sq2=cv::sum(square2);
       int B = getTickCount(); 
        int c=B-A;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-03-28 13:46:32 -0500

xaffeine gravatar image

updated 2014-03-28 15:59:20 -0500

There is a lot more memory traffic in the second version of the code. Your arithmetic operations are simple enough that they take almost no time compared to the memory operations. Specifically, your calls to multiply() and sum() cause extra memory writes and reads, proportional to the size of your matrix.

In any situation where speed is most important, you need to be careful how much memory access you do.

edit flag offensive delete link more

Comments

dear xaffeine, thank your answer,i try the other method again.

wuling gravatar imagewuling ( 2014-03-29 10:53:51 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-03-28 12:11:48 -0500

Seen: 89 times

Last updated: Mar 28 '14