any suggestions to make this code faster
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Daniel Shub
el 11 de En. de 2012
@huda, Please combine this question with your other similar question.
Respuestas (1)
Daniel Shub
el 10 de En. de 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 comentarios
Daniel Shub
el 11 de En. de 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
Ver también
Categorías
Más información sobre Time Series Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!