any suggestions to make this code faster

1 view (last 30 days)
huda nawaf
huda nawaf on 10 Jan 2012
hi,
any suggestions to make this code faster, where if take just noofusers 50, it take three or more to complete:
%%%%%%%%%%%%%%%%%%%%%
targetdir = 'd:\matlab11\r2011a\bin\training_set';
nofusers=480189;
targetfiles = '*.txt';
fileinfo = dir(fullfile(targetdir, targetfiles));
f5=fopen('matlab11\r2011a\bin\netflix\un1-17.txt');
z5=fscanf(f5,'%d');
fclose(f5);
for j=1:nofusers
k=1;
for i = 1:17770
thisfilename = fullfile(targetdir, fileinfo(i).name);
f = fopen(thisfilename,'r');
c = textscan(f, '%f %f %s', 'Delimiter', ',', 'headerLines', 1);
fclose(f);
c1 = c{1}; a=find(c1==z5(j));
if ~isempty(a)
c3=c{3};
format long
dat=round(datenum(c3,'yyyy-mm-dd'));
array(j,k)=i;
array(j,k+1)=dat(a);
k=k+2;
else
continue;
end; end; end;
thanks for any advice
  1 Comment
Daniel Shub
Daniel Shub on 11 Jan 2012
@huda, Please combine this question with your other similar question.

Sign in to comment.

Answers (1)

Daniel Shub
Daniel Shub on 10 Jan 2012
You should preallocate array to the correct size before the loop. It looks like you could also use a parfor for the first loop. You don't need "format long" in the inner loop (or at all). There are much faster things than datenum (have a search on this site).
  3 Comments
Daniel Shub
Daniel Shub on 11 Jan 2012
In the past, preallocation can considerably speed up code. r2011a is supposed to be better without preallocation, but I haven't tested it and have seen rumors that it is not. I think preallocating the rows will help. Currently, you increase the number of columns by 1, whenever you exceed the current number of columns. You can block allocate and increase it by 10, 100, 1000 ... This will also help speed things up
huda nawaf
huda nawaf on 14 Jan 2012
hi,
please , I did not understand what u mean y this:
Currently, you increase the number of columns by 1, whenever you exceed the current number of columns. You can block allocate and increase it by 10, 100, 1000 ... This will also help speed things up
I hope to give me more details
thanks

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!