Suggestion for rewrite of the for loop and while loop

2 visualizaciones (últimos 30 días)
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 comentarios
Jason Ross
Jason Ross el 25 de Abr. de 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 el 25 de Abr. de 2011
I m just dealing with 100kB data yet i needs around 15 mins to send and receive, Could you have any better suggestion?

Iniciar sesión para comentar.

Respuesta aceptada

Matt Fig
Matt Fig el 25 de Abr. de 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 comentarios
Matt Fig
Matt Fig el 25 de Abr. de 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 el 25 de Abr. de 2011
Deleted comment after Oleg's:
MrPuzzled Marc: Can you describe more about pre-allocation?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Historical Contests en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by