%// SETTINGS
Folder='C:\tmpSCI';
Nexecution=5;
Nponavljanja=100;
Nrekurzije=3;
NponavljanjaDatoteke=10;

for executor=1:Nexecution
	ct0=cputime;
    fs=string(Folder)+'\MATLAB-Xrez'+string(executor)+'.txt';
   	fname = fopen(fs,'w');

	NS=[500000,1000000,2000000,4000000];
	NN=size(NS); Nc=NN(2);

	fprintf(fname,'Repetitions=%d\n',Nponavljanja);
	fprintf(fname,'Function\tTotal\tAvg\tResult\n');
	for i=1:Nc 
		[r,t]=sumLoop(NS(i),Nponavljanja);
		fprintf(fname,'sumLoop(%d)\t%f\t%f\t%f\n',NS(i),t(4),t(1),r(1));
		[r,t]=sumVectLoop(NS(i),Nponavljanja);
		fprintf(fname,'sumVectLoop(%d)\t%f\t%f\t%f\n',NS(i),t(4),t(1),r(1));
		[r,t]=sumFLoop(NS(i),1,Nponavljanja);
		fprintf(fname,'sumFLoop(%d,1)\t%f\t%f\t%f\n',NS(i),t(4),t(1),r(1));
	%	[r,t]=sumFLoopFor(NS(i),1,Nponavljanja);
	%	fprintf(fname,'sumFLoopFor(%d,1)\t%f\t%f\t%f\n',NS(i),t(4),t(1),r(1));
	%	[r,t]=sumVectFLoop(NS(i),1,Nponavljanja);
	%	fprintf(fname,'sumVectFLoop(%d,1)\t%f\t%f\t%f\n',NS(i),t(4),t(1),r(1));
    end
%  fprintf(fname,'\n');
% 
% 	for i=1:Nc 
% 		[r,t]=sumFLoop(NS(i)*0.05,0.05,Nponavljanja);
% 		fprintf(fname,'sumFLoop(%d,0.05)\t%f\t%f\t%f\n',NS(i),t(4),t(1),r(1));
% 		[r,t]=sumFLoopFor(NS(i)*0.05,0.05,Nponavljanja);
% 		fprintf(fname,'sumFLoopFor(%d,0.05)\t%f\t%f\t%f\n',NS(i),t(4),t(1),r(1));
% 		[r,t]=sumVectFLoop(NS(i)*0.05,0.05,Nponavljanja);
% 		fprintf(fname,'sumVectFLoop(%d,0.05)\t%f\t%f\t%f\n',NS(i),t(4),t(1),r(1));
% 	end
% 	fprintf(fname,'\n');
% 
% 	[r,t]=fiboNR(10,Nponavljanja);
% 	fprintf(fname,'fiboNR(10)\t%f\t%f\t%f\n',t(4),t(1),r(1));
% 	[r,t]=fiboNR(100,Nponavljanja);
% 	fprintf(fname,'fiboNR(100)\t%f\t%f\t%f\n',t(4),t(1),r(1));
% 	[r,t]=fiboNR(1000,Nponavljanja);
% 	fprintf(fname,'fiboNR(1000)\t%f\t%f\t%f\n',t(4),t(1),r(1));
% 	[r,t]=fiboNR(1476,Nponavljanja);
% 	fprintf(fname,'fiboNR(1476)\t%f\t%f\t%f\n',t(4),t(1),r(1));
% 
% 	%for i=10:35
% 	%    [r,t]=fiboR(i,Nrekurzije);
% 	%    fprintf(fname,'fiboR(%d,%dx)\t%f\t%f\t%f\n',i,Nrekurzije,t(4),t(1),r(1));    
% 	%end
% 	fprintf(fname,'\n');
% 
% 	NS=[100,500,1000,2000];
% 	NN=size(NS); Nc=NN(2);
% 	for i=1:Nc
% 		[r,t]=mtxGenMN(NS(i),NS(i),3,Nponavljanja);
% 		fprintf(fname,'mtxGenMN(%d,%d,3)\t%f\t%f\t\n',NS(i),NS(i),t(4),t(1)); 
% 		[r,t]=mtxVectGenMN(NS(i),NS(i),3,Nponavljanja);
% 		fprintf(fname,'mtxVectGenMN(%d,%d,3)\t%f\t%f\t\n',NS(i),NS(i),t(4),t(1)); 
% 		if (i<Nc) 
% 			[r,t]=mtxGenMN(NS(i),NS(i)*2,3,Nponavljanja);
% 			fprintf(fname,'mtxGenMN(%d,%d,3)\t%f\t%f\t\n',NS(i),NS(i)*2,t(4),t(1)); 
% 			[r,t]=mtxVectGenMN(NS(i),NS(i)*2,3,Nponavljanja);
% 			fprintf(fname,'mtxVectGenMN(%d,%d,3)\t%f\t%f\t\n',NS(i),NS(i)*2,t(4),t(1));  
% 		end
% 	end
% 	fprintf(fname,'\n');
% 
% 	for i=1:Nc
% 		[r,t]=mtxGenNNPow(NS(i),3,NS(i)/2,Nponavljanja);
% 		fprintf(fname,'mtxGenNNPow(%d,3,%f)\t%f\t%f\t\n',NS(i),NS(i)/2,t(4),t(1)); 
% 		[r,t]=mtxVectGenNNPow(NS(i),3,NS(i)/2,Nponavljanja);
% 		fprintf(fname,'mtxVectGenNNPow(%d,3,%f)\t%f\t%f\t\n',NS(i),NS(i)/2,t(4),t(1)); 
% 		[r,t]=mtxGenNNPow(NS(i),11,NS(i)/2,Nponavljanja);
% 		fprintf(fname,'mtxGenNNPow(%d,11,%f)\t%f\t%f\t\n',NS(i),NS(i)/2,t(4),t(1)); 
% 		[r,t]=mtxVectGenNNPow(NS(i),11,NS(i)/2,Nponavljanja);
% 		fprintf(fname,'mtxVectGenNNPow(%d,11,%f)\t%f\t%f\t\n',NS(i),NS(i)/2,t(4),t(1));   
% 	end
% 	fprintf(fname,'\n');
% 
% 	NS=[100,1000,2000];
% 	NN=size(NS); Nc=NN(2);
% 	for i=1:Nc
% 		[r,t]=rndMtxMulti(NS(i),NS(i)*2,Nponavljanja);
% 		fprintf(fname,'rndMtxMulti(%d,%d)\t%f\t%f\t\n',NS(i),NS(i)*2,t(4),t(1)); 
% 		[r,t]=rndMtxMulti(NS(i)*2,NS(i),Nponavljanja);
% 		fprintf(fname,'rndMtxMulti(%d,%d)\t%f\t%f\t\n',NS(i)*2,NS(i),t(4),t(1)); 
% 		[r,t]=rndMtxMulti(NS(i),NS(i),Nponavljanja);
% 		fprintf(fname,'rndMtxMulti(%d,%d)\t%f\t%f\t\n',NS(i),NS(i),t(4),t(1)); 
% 	end
% 	fprintf(fname,'\n');
% 
% 	for i=1:Nc
% 		[r,t]=rndMtxDivBack(NS(i),Nponavljanja);
% 		fprintf(fname,'rndMtxDivBack(%d)\t%f\t%f\t\n',NS(i),t(4),t(1)); 
% 		[r,t]=rndMtxDivInv(NS(i),Nponavljanja);
% 		fprintf(fname,'rndMtxDivInv(%d)\t%f\t%f\t\n',NS(i),t(4),t(1)); 
% 	end
% 	fprintf(fname,'\n');
% 
% 	for i=1:Nc
% 		[r,t]=rndDet(NS(i),Nponavljanja);
% 		fprintf(fname,'rndDet(%d)\t%f\t%f\t\n',NS(i),t(4),t(1)); 
% 		[r,t]=rndDet0(NS(i),Nponavljanja);
% 		fprintf(fname,'rndDet0(%d)\t%f\t%f\t\n',NS(i),t(4),t(1)); 
% 		[r,t]=rndDet0X(NS(i),Nponavljanja);
% 		fprintf(fname,'rndDet0X(%d)\t%f\t%f\t\n',NS(i),t(4),t(1)); 
% 	end
% 	fprintf(fname,'\n');

    NS=[500000,1000000,2000000,4000000];
	NN=size(NS); Nc=NN(2);


	for i=1:Nc
		[r,t]=rndSort(NS(i),Nponavljanja);
		fprintf(fname,'rndSort(%d)\t%f\t%f\t\n',NS(i),t(4),t(1)); 
		[r,t]=rndSortRound(NS(i),Nponavljanja);
		fprintf(fname,'rndSortRound(%d)\t%f\t%f\t\n',NS(i),t(4),t(1)); 
	end
	fprintf(fname,'\n');

% 	NS=[100,1000,3000];
% 	NN=size(NS); Nc=NN(2);
% 
% 	for i=1:Nc
% 		[tsave,tload,fs]=rndMtxSaveLoad(NS(i),NponavljanjaDatoteke);
% 		fprintf(fname,'rndMtxSaveLoad(%d)\n',NS(i));
% 		fprintf(fname,'Save\t%f\t%f\n',tsave(4),tsave(1));
% 		fprintf(fname,'Load\t%f\t%f\n',tload(4),tload(1));
% 		fprintf(fname,'FileSize\t%f\t%f\n\n',fs(1),fs(2));
% 	end
% 	fprintf(fname,'\n');
% 
% 	[r,t]=sigAMSinFFT(100,1000,5,Nponavljanja,0);
% 	fprintf(fname,'sigAMSinFFT(100,1000,5,0)\nGen\t%f\t%f\n',t(4),t(1));
% 	[r,t]=sigAMSinFFT(100,4000,5,Nponavljanja,0);
% 	fprintf(fname,'sigAMSinFFT(100,4000,5,0)\nGen\t%f\t%f\n',t(4),t(1));
% 
% 	[r,t]=sigAMSinFFT(100,1000,50,Nponavljanja,0);
% 	fprintf(fname,'sigAMSinFFT(100,1000,50,%d,0)\nGen\t%f\t%f\n',Nponavljanja,t(4),t(1));
% 	[r,t]=sigAMSinFFT(100,4000,50,Nponavljanja,0);
% 	fprintf(fname,'sigAMSinFFT(100,4000,50,%d,0)\nGen\t%f\t%f\n',Nponavljanja,t(4),t(1));
 	ct1=cputime;
	fprintf(fname,'CPUTime\t%f',ct1-ct0);

end


function [R,RT]=sumLoop(N,rep)
    x=0; T=0; avgT=0;
    R=zeros(rep); RT=zeros(4); 
    % 29.03.2019 - ne radi u scilabu ugnjezdivanje tic toc pa moram sam ukupno 
    tic;
    for j=1:rep
        for i=1:N
            x=x+i*i;
        end        
        R(j)=x;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;
end

function [R,RT]=sumVectLoop(N,rep)
    x=0; T=0; avgT=0;
    R=zeros(rep); RT=zeros(4);
    tic;
    for j=1:rep
        orgVect=1:N;
        orgKv=orgVect.*orgVect;
        x=sum(orgKv);
        R(j)=x;
    end
    T=toc;   
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;
end

function [R,RT]=sumVectFLoop(N,delta,rep)
    x=0; T=0; avgT=0;
    R=zeros(rep); RT=zeros(4);
    tic;
    for j=1:rep
        orgVect=1:delta:N;
        orgKv=orgVect.*orgVect;
        x=sum(orgKv);
        R(j)=x;
    end
    T=toc;   
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;
end

function [R,RT]=sumFLoop(N,delta,rep)
    x=0; T=0; avgT=0;
    R=zeros(rep); RT=zeros(4);
    tic;
    for j=1:rep
        i=1;
        while (i<N)
            x=x+i*i;
            i=i+delta;
        end
        R(j)=x;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;
end

function [R,RT]=sumFLoopFor(N,delta,rep)
    x=0; T=0; avgT=0;
    R=zeros(rep); RT=zeros(4);
    tic;
    for j=1:rep
        for i=1:delta:N
            x=x+i*i;
        end
        R(j)=x;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;
end

function [R,RT]=fiboNR(N,rep)
    % nerekurzivni fibonacci
    T=0; avgT=0;
    R=zeros(rep); RT=zeros(4);
    tic;
    for j=1:rep
        fib=zeros(N+1);
        for n=1:N
            if (n==1) || (n==2)  fib(n)=1;
            else fib(n)=fib(n-1)+fib(n-2);
            end
        end
        R(j)=fib(N);
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;     
end

function x=fibRtmp(N)
    if (N<=2)  x=1; else x=fibRtmp(N-1)+fibRtmp(N-2);
    end
end

function [R,RT]=fiboR(N,rep)
    avgT=0; T=0;
    R=zeros(rep); RT=zeros(4);
    tic;
    for j=1:rep
        Rlast=fibRtmp(N);
        R(j)=Rlast;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;      
end

function [R,RT]=mtxGenMNOld(M,N,F,rep)
    avgT=0; T=0;
    mtxR=0;
    RT=zeros(4);
    tic;
    for j=1:rep
        clear mtxR;
        for row=1:M
            for col=1:N
                mtxR(row,col)=(row*col)/F; 
            end
        end
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;    
    R=mtxR;  
end

function [R,RT]=mtxGenMN(M,N,F,rep)
    avgT=0; T=0;
    mtxR=0;
    RT=zeros(4);
    tic;
    for j=1:rep
        mtxR=zeros(M,N);
        for row=1:M
            for col=1:N
                mtxR(row,col)=(row*col)/F; 
            end
        end
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;    
    R=mtxR;  
end

function [R,RT]=mtxVectGenMN(M,N,F,rep)
    avgT=0; T=0;
    mtxR=0;
    RT=zeros(4);
    tic;
    for j=1:rep
        r=1:M; c=1:N;
        mtxR=r'*c;
        mtxR=mtxR/F;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;    
    R=mtxR;  
end

function [R,RT]=mtxGenNNPow(N,X,F,rep)
    avgT=0; T=0;
    mtxR=0;
    RT=zeros(4);
    tic;
    for j=1:rep
        mtxR=zeros(N,N);
        for row=1:N
            for col=1:N
                mtxR(row,col)=(row*col)/F; 
            end
        end
        mtxRpow=mtxR^X;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;    
    R=mtxRpow;  
end

function [R,RT]=mtxVectGenNNPow(N,X,F,rep)
    avgT=0; T=0;
    mtxR=0;
    RT=zeros(4);
    tic;
    for j=1:rep
        r=1:N;
        mtxR=r'*r;
        mtxR=mtxR/F;
        mtxRpow=mtxR^X;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;    
    R=mtxRpow;  
end

function [R,RT]=rndMtxMulti(M,N,rep)
    % random generator matrica MxN i NxM plus mnozenje
    avgT=0; T=0;
    R=0;
    RT=zeros(4);
    tic;
    for j=1:rep
        A=rand(M,N);
        B=rand(N,M);
        C=A*B;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;       
end

function [R,RT]=rndMtxDivBack(N,rep)
    avgT=0; T=0;
    R=0;
    RT=zeros(4);
    tic;
    for j=1:rep
        A=rand(N,N);
        b=rand(N,1);
        X=A\b;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;      
end


function [R,RT]=rndMtxDivInv(N,rep)
    avgT=0; T=0;
    R=0;
    RT=zeros(4);
    tic;
    for j=1:rep
        A=rand(N,N);
        b=rand(N,1);
        X=inv(A)*b;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;       
end

function [R,RT]=rndDet(N,rep)
    avgT=0; T=0;
    R=zeros(rep); 
    RT=zeros(4);
    tic;
    for j=1:rep
        A=rand(N,N);
        dA=det(A);
        R(j)=dA;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T; 
end

function [R,RT]=rndDet0(N,rep)
    avgT=0; T=0;
    R=zeros(rep); 
    RT=zeros(4);
    tic;
    for j=1:rep
        A=rand(N,N);        
        c1=round(N*rand());
       % c2=round(N*rand());
        %fprintf('%d. Mijenjam %d sa %d\n',j,c1,c2);
        if (c1==0)  c1=1; end
       % if (c2==0)  c2=1; end
       % A(:,c2)=A(:,c1);    % izjednacim stupce, determinanta bi trebala postati 0!
        A(:,c1)=0;
        dA=det(A);
        R(j)=dA;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;       
end

function [R,RT]=rndDet0X(N,rep)
    avgT=0; T=0;
    R=zeros(rep); 
    RT=zeros(4);
    tic;    
    for j=1:rep
        zrow=zeros(1,N);
        zcol=zeros(N,1);
        A=rand(N,N);        
        c1=round(N*rand());
        if (c1==0)  c1=1; end
        A(:,c1)=0;
        is0=0;
        for r=1:N
            if (A(:,r)==zcol)  is0=1; end
            if (A(r,:)==zrow)  is0=1; end
            if (is0==1)  break; end
        end
        if (is0==1)  dA=0; else dA=det(A); end
        R(j)=dA;
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;       
end

function [R,RT,O]=rndSort(N,rep)
    avgT=0; T=0;
    R=zeros(rep); 
    O=zeros(rep); 
    RT=zeros(4);
    tic;
    for j=1:rep
        A=rand(N,1);
        O(j)=A(1);
        B=sort(A);
        R(j)=B(1);
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;       
end

function [R,RT,O]=rndSortRound(N,rep)
    avgT=0; T=0;
    O=zeros(rep); 
    R=zeros(rep); 
    RT=zeros(4);
    tic;   
    for j=1:rep
        A=rand(N,1);
        A=1000*A; A=round(A);
        O(j)=A(1);
        B=sort(A);
        R(j)=B(1);
    end
    T=toc;
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T;       
end

function [TSave,TLoad,Fsize]=rndMtxSaveLoad(N,rep)
    % 29.03. umjesto rand stavim pocetni rand, a onda sve ide +1 u petlji (da imam ralniji save time)
    totSave=0; totLoad=0;
    avgTSave=0; avgTLoad=0;
    TSave=zeros(4); TLoad=zeros(4);
    Fsize=zeros(rep);
    A=rand(N,N);
    tic;
    for j=1:rep% trazim N-ti broj
        A=A+j;
        tmpFn='C:\tmpSCI\dat'+string(j);%+'.mat';
        save(tmpFn,'A');
        fs=dir(tmpFn+'.mat'); fss=fs.bytes; Fsize(j)=fss;
    end
    totSave=toc;
    tic;
    for j=1:rep 
        tmpFn='C:\tmpSCI\dat'+string(j);%'.dat';
        load(tmpFn,'A');
    end
    totLoad=toc;
    avgTSave=totSave/rep;
    avgTLoad=totLoad/rep;
    TSave(1)=avgTSave;
    TSave(4)=totSave;
    TLoad(1)=avgTLoad;
    TLoad(4)=totLoad;
end

function [R,RT]=sigAMSinFFT(f0,fc,N,rep,draw)
    R=0;x=0; avgT=0; T=0;
    minT=0; maxT=0;
    RT=zeros(4);    
    % f0 frekvencija originalnog signala
    % fc frekvencija nositelja
    % amplituda izmedu -1 - 1
    % N koliko T zelim uzeti kao generator
    fu=10*fc;
    T0=1/f0;
    Tall=N*T0;
    Tdelt=1/fu;    
    t=0:Tdelt:Tall;  
    tmp=size(t);  
    Nuzoraka=tmp(2);
    fftOd=0;    
    fftDelt=fu/Nuzoraka;
    fftDo=fu-fftDelt;
    fftX=fftOd:fftDelt:fftDo;
    ntmp=size(fftX);
    Nfft=ntmp(2);
    tic; 
    for j=1:rep
        sig0=sin(2*pi*f0*t);
        sigC=sin(2*pi*fc*t);
        
        sigAM=sigC.*(1+sig0);
        fftAM=abs(fft(sigAM));    
       
        if (draw==1) 
            clf;
            subplot(2,1,1);
            plot(t,sigAM,'c',t,sig0,'k');
            subplot(2,1,2);
            plot(fftX(1:Nfft/2),fftAM(1:Nfft/2));
        end                 
    end
    T=toc;    
    avgT=T/rep;
    RT(1)=avgT;
    RT(4)=T; 
end