Edit: I found that the MATLAB processor time is about the same as the C++ processor time, but still can't find a way to help the wall clock time of the C++ program. (About the same as the processor time printed below)
This might be an obvious result, but I've found that for determining eigenvalues and eigenvectors of a very large matrix (2k x 2k) matlab is about 3-4 times faster than LAPACK used in C++.
The following matlab code:
tic
A = 0:(2000*2000-1);
A = mod(A,4273);
A = reshape(A,[2000,2000]);
[U,D,W] = eig(A);
toc
runs in 8.3 seconds on my machine whereas the following c++ code:
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
// dgeev_ is a symbol in the LAPACK library files
extern "C" {
extern int dgeev_(char*,char*,int*,double*,int*,double*, double*, double*, int*, double*, int*, double*, int*, int*);
}
int main(){
clock_t t;
int n,m;
n = 2000;
m = 2000;
double *data;
data = new double[n*m];
for(int i = 0; i < n*m; i++){
data[i] = i % 4273;
}
char Nchar='V';
double *eigReal=new double[n];
double *eigImag=new double[n];
double *vl=new double[n*m];
double *vr=new double[n*m];
int ld=n;
int lwork=6*n;
double *work=new double[lwork];
int info;
//print_array(data,n,m);
// calculate eigenvalues using the DGEEV subroutine
cout << "starting" << endl;
t = clock();
dgeev_(&Nchar,&Nchar,&n,data,&n,eigReal,eigImag,
vl,&ld,vr,&ld,
work,&lwork,&info);
t = clock() - t;
cout << "done" << endl;
cout << "time taken: " << ((float)t)/CLOCKS_PER_SEC << endl;
delete [] data;
delete [] eigReal;
delete [] eigImag;
delete [] work;
return 0;
}
requires 29.05 seconds on my machine.
Is it possible that MATLAB is just taking advantage of my hardware better?