OpenCV 4.1.1整体性能下降

时间:2019-12-13 13:38:48

标签: c++ opencv

将OpenCV从2.4.11升级到4.1.1时,我们注意到系统的整体性能下降。在代码的某些部分中,执行时间最多减少了25%。 (而OpenCV测试性能并没有表​​现出相同的性能损失。)

研究似乎表明,链接到OpenCV 4.1.1时,动态分配(所有分配,不仅与OpenCV对象有关!)都变慢了。

我们可以使用仅执行非OpenCV操作的基本程序进行重现(垫子分配超出了基准测试范围,此处只是为了确保链接)。 代码如下:

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <ch_cvl\timer.h>
#include <opencv2/opencv.hpp>

int main()
{
       using namespace std;

       vector<int> g1;

       /*implementation of openCV matrix in order to link OpenCV lib*/
       cv::Mat m(10, 10, CV_8UC1, 1);
       cv::Mat m2 = m; //this is needed to ensure the compiler does not remove the OpenMP 'm' during the optimization phase

       /*Cognex accurate timer*/
       double totalTimeInSeconds = 0.;                                           
       ccTimer oCognexTimer(true);

       /*computation loop*/
       for (int j = 0; j < 100000; j++)
       {
             for (int i = 1; i <= 1000000; i++)
                    g1.push_back(i); // ! dynamic allocation

             for (auto i = g1.begin(); i != g1.end(); ++i)
             {
                    *i++;
             }
             g1.clear();
       }

       oCognexTimer.stop(); // stop the timer
       totalTimeInSeconds = oCognexTimer.sec();

       ofstream ofs;                                                             
       ofs.open("result.txt", ofstream::out | ofstream::app);
       if (!ofs.fail())                                                                 
             ofs << totalTimeInSeconds << "(s)" << endl;
       cout << totalTimeInSeconds << "(s)" << endl;
       return 0;
}

我们运行了这个测试程序:

  1. 没有OpenCV ,删除了两条“ cv :: Mat…”行。 (未链接OpenCV库) 在这种情况下,所经过的时间是计算循环的近似时间。我的计算机(Intel i7,8GB内存)上的 23.1s
  2. 使用 OpenCV 2.4.1 经过的时间是计算循环约。 23.5秒
  3. 使用 OpenCV 4.1.1 经过的时间是计算循环约。 26.1秒

然后,我们运行了类似的计算循环,但是没有动态分配。现在的代码如下:

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <ch_cvl\timer.h>
#include <opencv2/opencv.hpp>

int main()
{
       using namespace std;

       vector<int> g1(1000000); //static allocation

       /*implementation of openCV matrix in order to link OpenCV lib*/
       cv::Mat m(10, 10, CV_8UC1, 1);
       cv::Mat m2 = m; //this is needed to ensure the compiler does not remove the OpenMP 'm' during the optimization phase

       /*Cognex accurate timer*/
       double totalTimeInSeconds = 0.;
       ccTimer oCognexTimer(true);

       /*computation loop*/
       for (int j = 0; j < 100000; j++)
       {
             for (int i = 1; i <= 1000000; i++)
                    g1[i] = i;

             for (auto i = g1.begin(); i != g1.end(); ++i)
             {
                    *i++;
             }
             g1.clear();
       }

       oCognexTimer.stop(); // stop the timer
       totalTimeInSeconds = oCognexTimer.sec();

       ofstream ofs;
       ofs.open("result.txt", ofstream::out | ofstream::app);
       if (!ofs.fail())
             ofs << totalTimeInSeconds << "(s)" << endl;
       cout << totalTimeInSeconds << "(s)" << endl;
       return 0;
}

测得的延迟为:

  1. 没有OpenCV ,删除了两条“ cv :: Mat…”行; (未链接OpenCV库)。 约 13.9秒
  2. 使用 OpenCV 4.1.1 经过的时间是计算循环约。 13.9秒

这种行为很奇怪,我们猜想我们缺少一些重要的东西,也许是在编译OpenCV 4的方式中。 是OpenCV 4.1.1重载了系统动态分配还是执行了一些可以解释观察到的速度下降的事情?


系统信息:

OpenCV 2.4.11和4.1.1是内置的Visual C ++ 2015(MSVC ++ 14.0)。

4.1.1的cmake配置命令为:

cmake.exe ../ -G "Visual Studio 14 2015 Win64" \
-DCMAKE_C_FLAGS_DEBUG="/O2 /MDd" -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_FLAGS_DEBUG="/O2 /MDd" -DCMAKE_CXX_FLAGS_RELEASE="/O2 /MD" \
-DCMAKE_C_FLAGS_RELEASE="/O2 /MD" -DWITH_IPP=ON -DWITH_CUDA=ON -DWITH_CUBLAS=ON \
-DWITH_CUFFT=ON -DCUDA_FAST_MATH=ON -DCUDA_ARCH_BIN="3.0 3.5 5.2 6.1 7.5" \
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-%OPENCV_VERSION%/modules \
-DBUILD_LIST=calib3d,core,cudev,cudaarithm,cudafilters,cudaimgproc,cudalegacy, \
cudawarping,feature2d,imgcodecs,imgproc,objdetect,photo,shape, \
stitching,superres,text,video,ximgproc \
-DWITH_EIGEN=ON -DWITH_OPENMP=OFF -DWITH_OPENCL=OFF -DWITH_QUIRC=OFF \
-DBUILD_PROTOBUF=OFF -DPYTHON_EXECUTABLE=OFF -DCMAKE_INSTALL_PREFIX=%DEST_DIR% \
-DBUILD_EXAMPLES=OFF -DBUILD_APPS=OFF -DBUILD_TESTS=OFF -DCPU_BASELINE=SSE4_2 \
-DCPU_DISPATCH=AVX,AVX2

测试程序是内置的Visual C ++ 2015(MSVC ++ 14.0)。

0 个答案:

没有答案