# Opencv matrix multiplication

**URL:** <https://forum.opencv.org/t/opencv-matrix-multiplication/4649>\
**Category:** C++\
**Tags:** mat, core\
**Created:** [August 7, 2021, 10:08am UTC](https://forum.opencv.org/t/opencv-matrix-multiplication/4649 "2021-08-07T10:08:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shahryar](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/shahryar/32/2847_2.png) [@Shahryar](https://forum.opencv.org/u/Shahryar)\
**Post date:** [August 7, 2021, 10:08am UTC](https://forum.opencv.org/t/opencv-matrix-multiplication/4649/1 "2021-08-07T10:08:13Z")

</div>

Hi everybody,  
i found that when i multiply vector in vector the run time is much more than when i multiply matrix in matrix in OpenCV 2.4.7  
consider that both of the vector and matrices has the same number of multiplication.  
for example:

```
 cv::Mat mat1(186, 186, CV_64FC1, cv::Scalar::all(20.));
  cv::Mat vec1(186, 1, CV_64FC1, cv::Scalar::all(20.));
  cv::Mat vec1_tr(1, 186, CV_64FC1, cv::Scalar::all(20.));
  int64_t matrix_time{ 0 };
  for (int i = 0; i < m_loop_count; ++i)
  {
    tick_tock mat_tick;
mat_tick.tick();
    cv::Mat out = (vec1_tr * mat1) * vec1;
   // 34782 multiplication
    matrix_time += mat_tick.tock();
  }
int mul_count = (186* 186) + 186; //34782 
  cv::Mat mat2(mul_count, 1, CV_64FC1, cv::Scalar::all(20.));
  cv::Mat mat2_tr(1, mul_count, CV_64FC1, cv::Scalar::all(20.));

  int64_t vec_time{ 0 };
  for (int i = 0; i < m_loop_count ; ++i)
  {
    tick_tock vec_tick;
			vec_tick.tick();
    cv::Mat out = mat2_tr * mat2;//34782 multiplication
    vec_time += vec_tick.tock();
  }

```

the matrix multiplication is much faster (2 times) than vector but both has 34782 multiplication.  
can anybody explain why?  
thank you.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 7, 2021, 11:30am UTC](https://forum.opencv.org/t/opencv-matrix-multiplication/4649/2 "2021-08-07T11:30:08Z")

</div>

> [@Shahryar](#):
>
> OpenCV 2.4.7

that died years ago.  
please throw it away, and update to recent 4.5.x (no discussion)
