fprintf('Benchmark suite 0.9b (.m) - Harris Georgiou (c) 2018\n\n');

clear all;

Nsz=2000;
Nlp=30;

Tsum=zeros(6,1);

fprintf('Matrix size: %d , Iterations: %d\nStarting tests...\n\n',Nsz,Nlp);


fprintf('[1]: Pseudo-inverse...\n');
for i=1:Nlp
	A=randn(Nsz,Nsz);
	tlap=tic;

	B=(A*A')^-1;		% calculate pseudo-inverse

	Tsum(1)=Tsum(1)+toc(tlap);
	fprintf('%g\n',B(1,1));
end
Tsum(1)=Tsum(1)/Nlp;


fprintf('[2]: Linear system...\n');
for i=1:Nlp
	A=randn(Nsz,Nsz);
	C=randn(Nsz,1);
	tlap=tic;

	B=A\C;				% solve the linear system

	Tsum(2)=Tsum(2)+toc(tlap);
	fprintf('%g\n',B(1,1));
end
Tsum(2)=Tsum(2)/Nlp;


fprintf('[3]: LSE regressor...\n');
for i=1:Nlp
	A=randn(Nsz,floor(Nsz/2));
	C=randn(Nsz,1);
	tlap=tic;

	B=A\C;				% estimate the LSE regressor

	Tsum(3)=Tsum(3)+toc(tlap);
	fprintf('%g\n',B(1,1));
end
Tsum(3)=Tsum(3)/Nlp;


fprintf('[4]: SVD...\n');
for i=1:Nlp
	A=randn(Nsz,Nsz);
	tlap=tic;

	B=svd(A);			% estimate the SVD

	Tsum(4)=Tsum(4)+toc(tlap);
	fprintf('%g\n',B(1,1));
end
Tsum(4)=Tsum(4)/Nlp;


fprintf('[5]: FFT...\n');
for i=1:Nlp
	A=randn(Nsz*Nsz,1);
	tlap=tic;

	B=fft(A,length(A));			% estimate the FFT

	Tsum(5)=Tsum(5)+toc(tlap);
	fprintf('%g\n',B(1,1));
end
Tsum(5)=Tsum(5)/Nlp;


fprintf('[6]: Bubblesort...\n');
for i=1:Nlp
	A=randn(floor(Nsz/2),1);
	tlap=tic;
	
	changed=true;		% bubblesort the vector
	while (changed)
		changed=false;
		for k=1:length(A)-1;
			if (A(k)>A(k+1))
				tmp=A(k+1);
				A(k+1)=A(k);
				A(k)=tmp;
				changed=true;
			end
		end
	end
	
	Tsum(6)=Tsum(6)+toc(tlap);
	fprintf('%g\n',A(1));
end
Tsum(6)=Tsum(6)/Nlp;


fprintf('\nRESULTS (sec):\nPinv=%g\nLin.sys=%g\nLSE.regr=%g\nSVD=%g\nFFT=%g\nBubblesort=%g\n\n',Tsum);
