# Efficient matrix operator

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 close merge delete

Sort by ยป oldest newest most voted

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.

more

( 2014-03-29 10:53:51 -0500 )edit

Official site

GitHub

Wiki

Documentation