0

I am preparing 16 cv::Mat variables with a given size and black pixel color as default on program start. To do that, I pass the variables one after the other to the following function. The loading process takes about 22 seconds in release mode (Windows, Visual Studio 2022) on an Intel W-2295 3.00 GHz. What does take so long, and how could I improve this time?

int init_mat(cv::Mat& mat)
{
    cv::Size size_from_config(80000, 3000);

    mat = cv::Mat(size_from_config, CV_8UC3, cv::Vec3b(0, 0, 0));

    return EXIT_SUCCESS;
}
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
gear
  • 181
  • 1
  • 11
  • 2
    how much RAM do you have, physically? how much ram does _one_ instance of that shape require (it's 720 MB)? did you notice a lot of swapping to disk? how do you determine that _this specific code_ takes how much time? how do you know it's not some other piece of code that uses up time? – Christoph Rackwitz Oct 28 '22 at 13:24
  • Ah thanks man, you probably already got me my answer. The machine I recently tested the code on, only has got 16 GB and the program utilize around 25GB of process storage. I was able to see that the the utilization took way longer compared with our 64GB production machine. So the delay is caused by the computer swapping the process storage to the disk, right? To your other question: I isolated the code and measured the duration with chrono – gear Oct 28 '22 at 13:43
  • 1
    Highly likely. This constructor basically allocates the data array and then calls `Mat::operator =` with a `Scalar` parameter. I'd be surprised if there was much left to optimize in something as fundamental at this point in time. | One possible improvement, but probably not relevant in the specific use-case you describe, would be to rewrite this into a call to `create` and an explicit scalar assignment. This would let you efficiently re-initialize `Mats` that already have the correct shape/type, by avoiding reallocation of the data array. i.e. do the initialization in-place. – Dan Mašek Oct 29 '22 at 16:22

0 Answers0