Prev: how to write the output data into text file
Next: how to generate random variable with constraint?
From: Adnane on 19 Jul 2010 21:13 Hi all, I'm building a simulation model of epidemics. I'm struggling with speed issues on some of my functions. I've gone through a fair amount of vectorization and such but remain well under the speed I'd like to achieve. I would appreciate any tip or trick to make this run faster. I've highlighted the lines that take the longest. Many thanks, Adnane Here's the code of one such function that does dispersal: function [y1, y2] = sub_spore_disp(metapop, param, spore_produced) grid=param.x_grid*param.y_grid; spore_arrival= zeros(size(param.path_geno,1),grid); spore_bank = zeros(grid,1); pres=(param.data(:,param.host_presence)>0); pop_inf=find(param.data(:,param.prevalence)); for i=1:size(pop_inf,1) pop=pop_inf(i,1); mdist=metapop(pop).dist; mpop_all=metapop(pop).pop_all(1,1); to_send=zeros(mpop_all,1); %%%%%%%%%%%%%%%%%% k=1:1:mpop_all; to_send(k,1)=pres(mdist(k,1), 1); send1=metapop(pop).send_path.*to_send; send1=horzcat(nonzeros(send1),find(send1)); size_send1=size(send1,1); if size_send1>0 temptimes= round(bsxfun(@times, send1(:,1)',spore_produced(:,i)));%%%%%%% for j=1:size_send1 pop_send=mdist(send1(j,2),1); spore_arrival(:,pop_send)= spore_arrival(:,pop_send)+ temptimes(:,j);%%%%%% spore_bank(pop_send,1)=1; end end end y1=spore_arrival; y2=sparse(spore_bank);
From: Darren Rowland on 19 Jul 2010 22:40 Without being able to run the code I can see only one thing you might change. Consider the following lines to_send=zeros(mpop_all,1); %%%%%%%%%%%%%%%%%% k=1:1:mpop_all; to_send(k,1)=pres(mdist(k,1), 1); Since k runs over the entire dimension of to_send there is no need to preallocate to_send to be zero. So you can replace these lines with to_send = pres(mdist(1:mpop_all,1),1); Hth Darren
From: Adnane on 19 Jul 2010 23:42 I've made the change and it resulted in some speed increase for the bit replaced. Thank you! Any suggestion regarding the remaining of the code ? Also why is preallocation not required sometimes ? From reading the Matlab Help, it seemed like a way to consistently gain processing speed.
From: Darren Rowland on 20 Jul 2010 03:55 Preallocation is helpful/necessary most of the time. However in your code you were preallocating the space using zeros and then filling ALL the space with another command. This would typically be picked up by M-lint, except the indexing complicates things. Another issue with preallocation which has come up on CSSM in the past is that the call to create an array of type double, e.g A = zeros(n); is slower than the explicit call A = zeros(n, 'double'); With regard to the other code portions I would need to have a better idea of what is happening. It may just be that it will take a long time regardless and if you really need a speed-up it might require compiled code. Darren
|
Pages: 1 Prev: how to write the output data into text file Next: how to generate random variable with constraint? |