any suggestions to make this code faster
조회 수: 3 (최근 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에서 Time Series Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!