Why is the execution time changing?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I have two versions of a code. Both versions are divided into two parts and the two versions differ for the first part, while the second part is identical in both. Here the time of execution reported by using tic toc: Version 1 -part 1: 0.03 sec -part 2: 0.19 sec
Version 2 -part 1: 0.05 sec -part 2: 0.48 sec
My question is: why the execution time of part 2 in the second version is higher, although the code of the second part is exactly the same in both versions? I'm probably doing a very stupid mistake but I can't see it. Here the code and attached the .mat files used:
clear all
r=30;
m=20;
n=6;
load data
load freq
load EC
load C
index=makeindex(n-1);
repindex=repmat(index,m,1);
param=[-0.7 0.6]';
j=1;
s=2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Version 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Part 1 (Different for the two versions)
tic
upp=[freq(:,1:3,ones(1,n)) zeros(size(freq,1),r,n)];
low=[freq(:,1:3,ones(1,n)) zeros(size(freq,1),r,n)];
datasub=data(m*j-(m-1):m*j,:);
gamma=param(1);
delta= param(2);
piece1=datasub(:,2+n+1:2+n+n-1)*gamma;
others=zeros(2^(n-1),n-1);
for i=1:n-1
behavothers=index;
behavothers(:,i)=[];
others(:,i)=sum(behavothers,2);
end
repothers=repmat(others,m,1);
piece2=delta*repothers;
piece1=kron(piece1, ones(2^(n-1),1));
obsprof= piece1+piece2;
epsi=kron(datasub(:,2+n+n-1+n-1+(n-1)*s-(n-1)+1:2+n+n-1+n-1+(n-1)*s), ones(2^(n-1),1));
prof=obsprof+epsi;
prof=(prof>0)==repindex;
sumequil=sum(prof,2);
vectorequil=(sumequil==(n-1)*ones(2^(n-1)*m,1));
toc
%%Part 2 (Identical for both versions)
tic
cumsumvectorequil= cumsum(vectorequil);
sumvectorequil=cumsumvectorequil(2^(n-1):2^(n-1):m*2^(n-1));
a=zeros((m-1),1);
for h=1:(m-1)
a(h)=sumvectorequil(h+1)-sumvectorequil(h);
end
sumvectorequilsep=[sumvectorequil(1); a];
ec=zeros(sumvectorequil(m), n+n-1+2);
previous=0;
for h=1:m
ec(previous+1:previous+sumvectorequilsep(h),1:n+1)=ones(sumvectorequilsep(h),1)*datasub(h,2:2+n);
previous=previous+sumvectorequilsep(h);
end
previous=0;
for h=1:m
indexequilibrium=find(vectorequil(2^(n-1)*h-2^(n-1)+1:2^(n-1)*h));
ec(previous+1:previous+size(indexequilibrium),1+n+1:1+n+n-1)=index(indexequilibrium,:);
previous=previous+size(indexequilibrium,1);
end
for h=1:size(ec,1)
for i=1:size(EC,1)
if all(ec(h,2:1+n+n-1)==EC(i,(1:n+n-1)))
ec(h,size(ec,2))=EC(i,n+n-1+2);
end
end
end
tmpupp = zeros(1, size(freq,1));
for i=1:size(ec,1)
for w=1:size(freq,1)
if all([ec(i,1) ec(i,1+n+n-1+1)]==[freq(w,1) freq(w,3)])
tmpupp(w)=1;
end
end
end
upp(:, s+3, j) = tmpupp';
tmplow = zeros(1, size(freq,1));
for i=1:size(ec,1)
for w=1:size(freq,1)
if all([ec(i,1) ec(i,1+n+n-1+1)]==[freq(w,1) freq(w,3)]) && sumvectorequilsep(ec(i),1)==1
tmplow(w)=1;
end
end
end
low(:, s+3, j) = tmplow';
toc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Version 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Part 1 (Different for the two versions)
tic
upp=[freq(:,1:3,ones(1,n)) zeros(size(freq,1),r,n)];
low=[freq(:,1:3,ones(1,n)) zeros(size(freq,1),r,n)];
datasub=data(m*j-(m-1):m*j,:);
gamma=param(1);
delta= param(2);
obsprofgreat= datasub(:,2+n+1:2+n+n-1)*gamma+delta*(n-2)*ones(m,n-1);
obsproflow= datasub(:,2+n+1:2+n+n-1)*gamma+zeros(m,n-1);
equilgreat=ones(m,n-1);
equillow=zeros(m,n-1);
responsegreat=(obsprofgreat+datasub(:,2+n+n-1+n-1+(n-1)*s-(n-1)+1:2+n+n-1+n-1+(n-1)*s)>0);
while any(abs( all(responsegreat==equilgreat,2)-ones(size(responsegreat,1),1)))==true
equilgreat=responsegreat;
others=zeros(m,n-1);
for i=1:n-1
behavothers=equilgreat;
behavothers(:,i)=[];
others(:,i)=sum(behavothers,2);
end
responsegreat=(datasub(:,2+n+1:2+n+n-1)*gamma+delta*others+datasub(:,2+n+n-1+n-1+(n-1)*s-(n-1)+1:2+n+n-1+n-1+(n-1)*s)>0);
end
responselow=(obsproflow+datasub(:,2+n+n-1+n-1+(n-1)*s-(n-1)+1:2+n+n-1+n-1+(n-1)*s)>0);
while any(abs( all(responselow==equillow,2)-ones(size(responselow,1),1)))==true
equillow=responselow;
others=zeros(m,n-1);
for i=1:n-1
behavothers=equillow;
behavothers(:,i)=[];
others(:,i)=sum(behavothers,2);
end
responselow=(datasub(:,2+n+1:2+n+n-1)*gamma+delta*others+datasub(:,2+n+n-1+n-1+(n-1)*s-(n-1)+1:2+n+n-1+n-1+(n-1)*s)>0);
end
repgreat=kron(responsegreat, ones(2^(n-1),1));
replow=kron(responselow, ones(2^(n-1),1));
sumgreat=kron(sum(responsegreat,2),ones(2^(n-1),1));
sumlow=kron(sum(responselow,2),ones(2^(n-1),1));
sumindex=repmat(sum(index,2),m,1);
vectorequil=((sumindex<sumgreat & sumindex>sumlow) | ((all(repgreat==repindex,2) | all(replow==repindex,2))));
toc
%%Part 2 (Identical for both versions)
tic
cumsumvectorequil= cumsum(vectorequil);
sumvectorequil=cumsumvectorequil(2^(n-1):2^(n-1):m*2^(n-1));
a=zeros((m-1),1);
for h=1:(m-1)
a(h)=sumvectorequil(h+1)-sumvectorequil(h);
end
sumvectorequilsep=[sumvectorequil(1); a];
ec=zeros(sumvectorequil(m), n+n-1+2);
previous=0;
for h=1:m
ec(previous+1:previous+sumvectorequilsep(h),1:n+1)=ones(sumvectorequilsep(h),1)*datasub(h,2:2+n);
previous=previous+sumvectorequilsep(h);
end
previous=0;
for h=1:m
indexequilibrium=find(vectorequil(2^(n-1)*h-2^(n-1)+1:2^(n-1)*h));
ec(previous+1:previous+size(indexequilibrium),1+n+1:1+n+n-1)=index(indexequilibrium,:);
previous=previous+size(indexequilibrium,1);
end
for h=1:size(ec,1)
for i=1:size(EC,1)
if all(ec(h,2:1+n+n-1)==EC(i,(1:n+n-1)))
ec(h,size(ec,2))=EC(i,n+n-1+2);
end
end
end
tmpupp = zeros(1, size(freq,1));
for i=1:size(ec,1)
for w=1:size(freq,1)
if all([ec(i,1) ec(i,1+n+n-1+1)]==[freq(w,1) freq(w,3)])
tmpupp(w)=1;
end
end
end
upp(:, s+3, j) = tmpupp';
tmplow = zeros(1, size(freq,1));
for i=1:size(ec,1)
for w=1:size(freq,1)
if all([ec(i,1) ec(i,1+n+n-1+1)]==[freq(w,1) freq(w,3)]) && sumvectorequilsep(ec(i),1)==1
tmplow(w)=1;
end
end
end
low(:, s+3, j) = tmplow';
toc
2 个评论
回答(2 个)
the cyclist
2013-11-14
I recommend you analyze your code using the profiler.
>> doc profile
for details.
2 个评论
the cyclist
2013-11-15
I'm confused. The profiler doesn't show any timing difference between these two sections of code, but tic-toc does?
Matt J
2013-11-14
If you aren't doing so already, put these routines in a function mfile (not in a script mfile). Then redo the timing tests.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!