Lip*_*eka 5 arrays matlab loops execution-time
一些指示,将不胜感激.我正在阅读大约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');
Run Code Online (Sandbox Code Playgroud)
谢谢
下面的代码运行速度比原始代码快约 200 倍,并给出相同的结果。
当然,加速取决于输入数据的分布,我的假设可能不正确(我有 1000 个唯一 ID,平均每天 19 条记录)。
我还编写了一些代码来生成与我认为您的输入数据类似的数据。
% Generate Input data
ep = 100000;
isRepeatedDay = rand(1,ep) < 0.95;
day = cumsum(~isRepeatedDay);
unique_ids = 1:1000;
unique_id_indices = round(rand(ep,1)*length(unique_ids));
unique_id_indices(unique_id_indices < 1) = 1;
unique_id_indices(unique_id_indices > length(unique_id_indices) ) = length(unique_id_indices);
unique_id = unique_ids(unique_id_indices);
%Process the input data to find repeats
tic
repeat = zeros(ep,1);
[unique_values,~,indices] = unique(unique_id);
for uv_index = 1:length(unique_values)
uv = unique_values(uv_index);
uv_indices = find(indices == uv_index);
for i=1:length(uv_indices)-1
daysDifference = day(uv_indices(i+1)) - day(uv_indices(i));
if daysDifference <= 179
repeat(uv_indices(i),1) = 1;
end
end
end
toc
Run Code Online (Sandbox Code Playgroud)