Suggestion for rewrite of the for loop and while loop

2 vues (au cours des 30 derniers jours)
MrPuzzled Marc
MrPuzzled Marc le 25 Avr 2011
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 commentaires
Jason Ross
Jason Ross le 25 Avr 2011
Do you know how long you think it should take?
Do you have a performance limit you are required to meet?
MrPuzzled Marc
MrPuzzled Marc le 25 Avr 2011
I m just dealing with 100kB data yet i needs around 15 mins to send and receive, Could you have any better suggestion?

Connectez-vous pour commenter.

Réponse acceptée

Matt Fig
Matt Fig le 25 Avr 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 commentaires
Matt Fig
Matt Fig le 25 Avr 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
Matt Fig le 25 Avr 2011
Deleted comment after Oleg's:
MrPuzzled Marc: Can you describe more about pre-allocation?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Historical Contests dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by