矩陣操作比較:Armadillo,Eigen,OpenCV
阿新 • • 發佈:2019-02-03
// PerformanceTest.h #pragma warning(disable:4344) #define DEIGEN_NO_DEBUG #define DNDEBUG #include <emmintrin.h> #include <opencv.hpp> #include <vector> #include <iostream> #include <armadillo> #include <Eigen/Dense> #include "Timer.h" using namespace std; //------------------------------------------------------- // PerformanceTest.cpp #include "PerformanceTest.h" int main(void) { Timer timer; // timer double elapsedTime; // time in millisecond double res; // SAD value int i; // loop variable float bnd = 1e5; // loop times // Armadillo arma::mat armaA(4, 1); arma::mat armaB(4, 1); timer.start(); for (i = 0; i < bnd; ++i) { res = arma::accu(arma::abs(armaA - armaB)); //res = 0; //for (int idx = 0; idx < 4; ++idx) //{ // res += abs(armaA(idx, 0) - armaB(idx, 0)); //} } elapsedTime = timer.getElapsedTimeInMilliSec(); cout<<"arma time : "<<elapsedTime<<" ms"<<endl; // Eigen Eigen::Vector4d eiA; Eigen::Vector4d eiB; Eigen::Vector4d eiC; timer.start(); for (i = 0; i < bnd; ++i) { res = (eiA - eiB).cwiseAbs().sum(); //res = 0; //for (int idx = 0; idx < 4; ++idx) //{ // res += abs(eiA(idx,0) - eiB(idx, 0)); //} } elapsedTime = timer.getElapsedTimeInMilliSec(); cout<<"eigen time : "<<elapsedTime<<" ms"<<endl; // OpenCV cv::Mat ocvA(4, 1, CV_64F); cv::Mat ocvB(4, 1, CV_64F); timer.start(); for (i = 0; i < bnd; ++i) { res = cv::sum(cv::abs(ocvA - ocvB))[0]; //res = 0; //for (int idx = 0; idx < 4; ++idx) //{ // res += abs(ocvA.at<double>(idx, 0) - ocvB.at<double>(idx, 0)); //} } elapsedTime = timer.getElapsedTimeInMilliSec(); cout<<"opencv time : "<<elapsedTime<<" ms"<<endl; // pointer operation double *a = (double*)_mm_malloc(4 * sizeof(double), 16); double *b = (double*)_mm_malloc(4 * sizeof(double), 16); int len = ocvA.rows; timer.start(); for (i = 0; i < bnd; ++i) { res = 0; for (int idx = 0; idx < len; ++idx) { res += abs(a[idx] - b[idx]); } //cout<<"i = "<<i<<endl; } elapsedTime = timer.getElapsedTimeInMilliSec(); cout<<"array operation : "<<elapsedTime<<" ms"<<endl; // release resource _mm_free(a); _mm_free(b); return 0; }