将不胜感激一些指点。我正在阅读大约 1M 行数据,使用以下代码需要将近 24 小时。如何提高执行时间?
该数组Day
包含从开始的第 n 天的值,并且某一天有多个记录。该程序检查一个特定的 id(存储在 中unique_id
)是否在 180 天内重复。
%// calculating the number of repeats within 180 days
fid2 = 'data_050913/Unique_id_repeat_count1.xlsx';
fid1 = 'data_050913/data_050913_2000.csv';
fid_data = fopen(fid1);
data = fgetl(fid_data); %// the first line, title line
ep = 0; %// position point number
while 1
data = fgetl(fid_data);
if(length(data)<10)
break;
end
ep = ep+1;
id = find(data == ',');
unique_id(ep) = str2num(data(1:id(1)-1));
day(ep) = str2num(data(id(8)+1:id(9)-1));
end
repeat = zeros(ep,1);
tic
i = 1;
count = 0;
while i <= ep
j = i+1;
while ( (j<=ep) && (day(j)<= day(i)+179) )
if unique_id(i) == unique_id(j)
count = 1;
break;
end
j = j+1;
end
repeat(i,1) = count;
count = 0;
i = i+1;
end
toc
i = 1;
k = 1;
while i<=ep
count = repeat(i,1);
j=i;
while (day(j) == day(i))
count = repeat(j,1)+count;
j = j+1;
if j > ep
break;
end
end
day_final(k,1)= day(i);
repeat_final(k,1) = count;
k = k+1;
i = j;
end
xlswrite(fid2,day_final,'Repeat_Count','A2');
xlswrite(fid2,repeat_final,'Repeat_Count','B2');
谢谢