How can I reduce the execution time in the following code?
2 次查看(过去 30 天)
显示 更早的评论
I have my .dat file with 5crore hex values. I am extracting some of the values and applying bit operations and storing back to a variable called my_data. In order to reduce the memory conflicts I saved the variable my_data into z.mat file and I succeeded in the job what I want to do with a result of 14841871x1 double of my_data. But it is taking too long for execution of my matlab code and nearly 45minutes long. I am attaching my code here
read_pet_data;
i=1;
j=1;
k=1;
count = 0;
my_data=double(zeros((length(A)),1));
A=double(A);
filename='z.mat';
m = matfile(filename, 'Writable',true);
save(filename,'my_data','-v7.3');
while(i<length(A))
while A(i,1)==0
flag=0;
count = count+1;
i=i+1;
end
if count>=20 && A(i,1)==166 && A(i+1,1)==210 && flag ==0
j=i;
pack_length =(abs(A(j+3)*256+A(j+4))); %packet length is two bytes of data together so
my_data(k,1)= pack_length;
j=i+27;
i=j;
flag=1;
end
if flag ==1
if i+11 < length(A)
j=i;
load_count=0;
payload_2;
i=j;
end
end
i=i+1;
count=0;
end
last = find(my_data~=0, 1, 'last' ); %to find the index of last non-zero element
my_data = my_data(1:last+1); %saving till last non-zero element and since the sequence number is 0
my_data = double(my_data);
m.my_data = my_data;
E=(0:Ts:((length(my_data)-1)/Fs))';
F=[E my_data];
6 个评论
KL
2017-4-24
I don't know what you originally have inside 'z.mat'. If you don't load my data in the beginning and just use save function in the end, it will replace the original z.mat (assuming you are using the same file name). If you want to keep the other variables (if any) apart from the modified 'my_data', then I'd suggest to load 'z.mat' in the beginning of your program and after all your calculations, save it in the end.
回答(1 个)
KL
2017-4-24
编辑:KL
2017-4-24
From what I understand, you only have my_data in the matfile 'z' and you want to do some calculations and save the new my_data again into z.mat. You can load z.mat in the beginning and save it under the same name after your calculations.
I'd also advise using profile to see what's taking more time in your code. I do not understand why you have a second while loop there and I suppose this is the one taking so much time considering the size of A.
You could also use if..elseif (better practice). I do not understand your complete code but something like this should work.
profile on
load('z.mat')
while(i<length(A))
% code here
if(count>=20 && A(i,1)==166 && A(i+1,1)==210 && flag ==0)
% code here
elseif(flag==1 && i+11 <length(A))
% code here
end
% code here
end
% code here
save('z.mat')
profile viewer
Now you should be able to see the profiler window with the time consumed for every part of the code. From there you can figure how you can go further to optimize the code. Good luck!
KL.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Software Development Tools 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!