This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Why Matrix Add is slower than Matrix-Vector Multiplication?

Tags: None
(comma "," separated)
complexfilter
Registered Member
Posts
1
Karma
0
Why Matrix Add takes much longer than Matrix-Vector Multiplication?

Matrix Add only costs n^2 add, whereas Matrix-Vector Multiplication takes n*(n-1) add and n^2 multiplication.

However, in Eigen, Matrix Add takes twice the time as Matrix-Vector Multiplication does. Does there exists any option to speed up Matrix Add operation in Eigen?

Code: Select all
#include <eigen3/Eigen/Eigen>
#include <iostream>
#include <ctime>
#include <string>
#include <chrono>
#include <fstream>
#include <random>
#include <iomanip>

using namespace Eigen;
using namespace std;

int main()
{
const int l=100;
MatrixXf m=MatrixXf::Random(l,l);
MatrixXf n=MatrixXf::Random(l,l);
VectorXf v=VectorXf::Random(l,1);

MatrixXf qq=MatrixXf::Random(l,1);
MatrixXf pp=MatrixXf::Random(l,l);

auto start = chrono::steady_clock::now();
for(int j=0;j<10000;j++)
qq=m*v;
auto end = chrono::steady_clock::now();
double time_duration=chrono::duration_cast<chrono::milliseconds>(end - start).count();
std::cout << setprecision(6) << "Elapsed time in seconds : "<< time_duration/1000<< "s" << std::endl;
auto start1 = chrono::steady_clock::now();
for(int j=0;j<10000;j++)
pp=m+n;
auto end1 = chrono::steady_clock::now();
double time_duration1=chrono::duration_cast<chrono::milliseconds>(end1 - start1).count();
std::cout << setprecision(6) << "Elapsed time in seconds : "<< time_duration1/1000<< "s" << std::endl;
}


Test 1: Without any optimization:

compile command: g++-8 -test.cpp -o test

run command: ./test

Elapsed time in seconds : 0.323s

Elapsed time in seconds : 0.635s

Test 2: With -march=native optimization:

g++-8 test.cpp -march=native -o test

run command: ./test

Elapsed time in seconds : 0.21s

Elapsed time in seconds : 0.372s

Test 3: With -O3 optimization:

compile command: g++-8 -test.cpp -O3 -o test

run command: ./test

Elapsed time in seconds : 0.009s

Elapsed time in seconds : 0.016s

Test 4: With -march=native, -O3 optimization:

compile command: g++-8 -test.cpp -march=native -O3 -o test

run command: ./test

Elapsed time in seconds : 0.008s

Elapsed time in seconds : 0.016s


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]