speed-up the given code
Show older comments
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 Comments
Geoff Hayes
on 22 Jun 2018
Abhinav - without knowing anything about your uh_fun functions (I couldn't load them into my version of MATLAB) it may be difficult to suggest any improvements. When you say that your code is taking a lot of time, what do you mean? Does it take minutes or hours?
Pieter Hamming
on 22 Jun 2018
I get this error when opening the .mat:
D:\Research\Thesis_work\Structural_uncertainty\MatLab_codes\20180222\expconv.m>@(t)exp(-lambda(i)*t)*C(i)
Warning: Could not find appropriate function on path loading function handle
Pieter Hamming
on 22 Jun 2018
As the .mat isn't opening we can't do much, but I still may have a suggestion:
tall arrays are Matlab's way of handling large arrays without causing memory issues. As f_gamma has 200001 rows, this may be memory related. Consider changing into tall arrays.
Abhinav
on 22 Jun 2018
Why do you need to create all these function handles?
%for example
lambda = 1:10;
C = 1:10;
for j = 1:10
fh{j} = @(t)exp(-lambda(i)*t)*C(i)
end
%will create something like...
@(t)exp(-1*t) * 1
@(t)exp(-2*t) * 2
@(t)exp(-3*t) * 3
Abhinav
on 22 Jun 2018
Are Mjaavatten
on 22 Jun 2018
What is the problem? Since p is not given I add a line:
p = rand(size(lambda,1),1);
in your code. I then save it as abhinav.m and time it:
>> tic;abhinav;toc
Elapsed time is 0.023084 seconds.
Abhinav
on 22 Jun 2018
Geoff Hayes
on 22 Jun 2018
Abhinav - is the temp_uh_fun exactly like what you have shown us in the attached code (expconv.m) or is it something else? It is probably expensive to call exp so many times especially when your t is an array of 200001 elements. Can you reduce the number of calls to exp? For example, suppose you do
expT = exp(t);
Then to get exp(-1*t) then this should be the same as
expTA = 1 ./ expT;
and similarly for exp(-2*t) and exp(-3*t), these would be the same as
expTB = expTA .* expTA;
expTC = expTB .* expTA;
I'm not sure if that will be much faster, but you have reduced the number of calls to exp by two-thirds...
Abhinav
on 22 Jun 2018
Geoff Hayes
on 22 Jun 2018
have you posted the actual function that you are using or just an example? We need to see the actual code so that we can provide some hints on how to improve the performance.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!