Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

any suggestions to make this code faster

Asked by 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 on 11 Jan 2012

@huda, Please combine this question with your other similar question.

huda nawaf

Tags

Products

No products are associated with this question.

1 Answer

Answer by Daniel 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

huda nawaf on 11 Jan 2012

is preallocate make the code faster ?
if I know just the no. of rows , can do for ex. array(1:100,:)=0?

thanks

Daniel 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 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

Daniel

Contact us