# speed-up the given code

2 次查看（过去 30 天）
Abhinav 2018-6-22

The following code is taking a most of the time in my script and I have to run the script many times Is there a way to reduce the time for its execution.
t=0:200000;
lambda=[1,2,3;4,5,6;7,8,9;10,11,12];
for trans=1:size(lambda,1)
uh_fun{trans}=expconv(lambda(trans,:));
end
uh=zeros(length(t),1);
for i=1:length(uh_fun)
temp_uh_fun=uh_fun{i};
f_gamma=zeros(length(t),1);
for j=1:length(temp_uh_fun)
f_gamma=f_gamma+temp_uh_fun{j}(t)'; % this line take a lot of time
end
uh=uh+p(i)*f_gamma; % p is an array of scalars
end
I have added expconv function for reference. I need some suggestions to improve the performance. I have also attached a pdf which contains profiler analysis of anonymous function in expconv.
##### 13 个评论显示 12更早的评论隐藏 12更早的评论
Abhinav 2018-6-22

This is the actual function. Just the lambda values are arbitrary. I think that you are right about calls to exp taking a lot of time.

### 采纳的回答

OCDER 2018-6-22

This might be a variant of the xy-problem that @Stephen points out. http://xyproblem.info/
Solve X: you want to add the values for a function for all P, C, lambda values. Attempt with using function handles, called Solution Y.
Solve Y: you make a lot of function handles for all P, C, lambda sets. But this is too slow. You want a solution to make Solution Y faster, not seeing a faster solution for X.
%This is just an EXAMPLE. Check to make sure you get the right results
C = rand(1000, 1);
P = rand(1000, 1);
lambda = rand(1000, 1);
t = 1:10000;
%Solution Y approach : use handles
Ysum = zeros(1, length(t));
for i = 1:length(C)
fh = @(t) P(i)*exp(-lambda(i)*t) + C(i);
Ysum = Ysum + fh(t);
end
t2 = toc
%Solution X approach : use vectorized math
tic
Ysum = sum(bsxfun(@times, exp(-lambda*t), P) + C, 1);
t1 = toc % ~5 times faster
I notice you like using repmat and find. Note that use of repmat is slower than bsxfun. Also, if possible, avoid using find if dealing with logical indexing. For instance:
function uh_fun=expconv(lambda)
k=length(lambda);
% Lambda=repmat(lambda,k,1);
% temp_den=Lambda'-Lambda;
temp_den = lambda' - lambda; %faster
% ind=find(temp_den==0);
ind = temp_den == 0; %stick with logical indexing
if sum(ind(:)) > k % length(ind)>k %corrected this
error('atleast two of the pdfs to be convolved are identical')
else
% temp_den(temp_den==0)=1;
temp_den(ind) = 1; %no need to re-find temp_den == 0;
den=prod(temp_den);
nume=prod(lambda);
C=nume./den;
for i=1:k
uh_fun{i}=@(t)exp(-lambda(i)*t)*C(i); %Do you reallly have to make all these handles?
end
end
end
If this is still too slow, then you could try using parfor or mex functions.
##### 4 个评论显示 3更早的评论隐藏 3更早的评论
Abhinav 2018-6-22

I have another 'unitHydrograph' in which the slow code is written. unitHydrograph has changing inputs. I have declared other useful variables which are calculated by unitHydrograph and remain fixed as 'persistent' so that I am not recomputing all those. Using matrix computation in lambda has not helped though.
Thanks a lot for your help and suggestions for improving my code!!

### 类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by