any suggestions to make this code faster
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Daniel Shub
2012-1-10
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 个评论
Daniel Shub
2012-1-11
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!