# 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:

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 <=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;
}
```