Suggestion for rewrite of the for loop and while loop
1 view (last 30 days)
Show older comments
Hi, i have created a code. However, it takes very long execution time to execute the code.Can anyone suggest a better solution to improve on the code?Here is the code.
%To receive received=fgetl(s); %Get from serial port using fgetl
file=[file received]; %The received bits is joined together to form a stream of bits.
%To form back the file
size_byte=fix(size(file)/32);
M=size_byte(2);
Video=[];
for j=1:M %For loop to get the binary bits back to matrices format
b=16*(j);
a=-15+b;
Video= [Video;file(a:b)];
end
dec=bin2dec(Video);
fid1=fopen (Save_file, 'wb');
count=fwrite(fid1, dec, 'uint16');
I have run using the profiler and it stated is the fgetl and Video= [Video;file(a:b)];which cause alot of execution time.Can anyone help to suggest for a better solution?I need it urgently. Thanks.
3 Comments
Jason Ross
on 25 Apr 2011
Do you know how long you think it should take?
Do you have a performance limit you are required to meet?
Accepted Answer
Matt Fig
on 25 Apr 2011
The major improvement will come from Pre-allocating memory for Video instead of growing the data in the loop. Calculate the final size before the loop and use the ZEROS function to grab the memory before the loop.
5 Comments
Matt Fig
on 25 Apr 2011
What do you mean describe more? Did you run the example above? It does the same thing twice, once with pre-allocation and once without. What more do you need to know? Please be specific.
Matt Fig
on 25 Apr 2011
Deleted comment after Oleg's:
MrPuzzled Marc: Can you describe more about pre-allocation?
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!