How to speed up this script

조회 수: 2 (최근 30일)
Bram Surewaard
Bram Surewaard 2020년 8월 31일
답변: Vidhi Agarwal 2024년 12월 10일
Hey,
I am reading data from a text file using the following script:
tic
for i = 1:2:length(Data);
Data2(i).H0_SoundSpeed = typecast(uint32(hex2dec(cell2mat(Data{i,1}(1,65:68)))),'single');
Data2(i).H0_Points = hex2dec(cell2mat(Data{i,1}(1,135:136)));
Data2(i).R0_SectionName = convertCharsToStrings(char(hex2dec(Data{i,1}(1,137:138))));
Data2(i).R0_ScalingFactor = typecast(uint32(hex2dec(cell2mat(Data{i,1}(1,141:144)))),'single');
Data2(i).A2_SectionName = convertCharsToStrings(char(hex2dec(Data{i,1}(1,657:658))));
Data2(i).A2_AngleFirst = typecast(uint32(hex2dec(cell2mat(Data{i,1}(1,661:664)))),'single');
Data2(i).A2_ScalingFactor = typecast(uint32(hex2dec(cell2mat(Data{i,1}(1,665:668)))),'single');
Data2(i).I1_ScalingFactor = typecast(uint32(hex2dec(cell2mat(Data{i,1}(1,1209:1212)))),'single');
Data2(i).A2_AngleStep(1) = 0 ;
for j = 1 : Data2(i).H0_Points ;
factor = Data2(i).H0_SoundSpeed * Data2(i).R0_ScalingFactor / 2 ;
Data2(i).R0_Range(j) = factor * hex2dec(cell2mat(Data{i,1}(1, (2*j+143):(2*j+144))));
Data2(i).A2_AngleStepTemp(j) = hex2dec(cell2mat(Data{i,1}(1, (2*j+691):(2*j+692))));
Data2(i).A2_AngleStep(j) = Data2(i).A2_AngleFirst + (sum(Data2(i).A2_AngleStepTemp(1:(j)))*Data2(i).A2_ScalingFactor);
Data2(i).I1_Intensity(j) = Data2(i).I1_ScalingFactor * hex2dec(cell2mat(Data{i,1}(1, (2*j+1211):(2*j+1212))));
end
end
Data2 = rmfield(Data2, 'A2_AngleStepTemp' );
toc
It is not the most efficient way of reading it in but it works, however it is a bit slow.
I was wondering if anybody has some tips on how I can speed it up.
Thanks!

답변 (1개)

Vidhi Agarwal
Vidhi Agarwal 2024년 12월 10일
To improve the performance of your MATLAB script, try considering given optimization:
  • Preallocation: Preallocating memory for structures and arrays can significantly enhance performance by reducing the need for MATLAB to dynamically resize data structures during execution.
  • Vectorization: Replace loops with vectorized operations where possible.
  • If a calculation is repeated within a loop but yields the same result each time, try computing it once before the loop and store it in a variable.
Below is the revised version of the code with above consideration:
tic
% Preallocate the Data2 structure array with the anticipated size
Data2(length(Data)/2) = struct('H0_SoundSpeed', [], 'H0_Points', [], 'R0_SectionName', [], ...
'R0_ScalingFactor', [], 'A2_SectionName', [], 'A2_AngleFirst', [], ...
'A2_ScalingFactor', [], 'I1_ScalingFactor', [], 'A2_AngleStep', [], ...
'R0_Range', [], 'A2_AngleStepTemp', [], 'I1_Intensity', []);
% Iterate over Data with a step of 2
for i = 1:2:length(Data)
dataRow = Data{i,1}; % Extract the data row once to avoid repeated indexing
% Extract and convert values
Data2(i).H0_SoundSpeed = typecast(uint32(hex2dec(dataRow(65:68))), 'single');
Data2(i).H0_Points = hex2dec(dataRow(135:136));
Data2(i).R0_SectionName = convertCharsToStrings(char(hex2dec(dataRow(137:138))));
Data2(i).R0_ScalingFactor = typecast(uint32(hex2dec(dataRow(141:144))), 'single');
Data2(i).A2_SectionName = convertCharsToStrings(char(hex2dec(dataRow(657:658))));
Data2(i).A2_AngleFirst = typecast(uint32(hex2dec(dataRow(661:664))), 'single');
Data2(i).A2_ScalingFactor = typecast(uint32(hex2dec(dataRow(665:668))), 'single');
Data2(i).I1_ScalingFactor = typecast(uint32(hex2dec(dataRow(1209:1212))), 'single');
% Preallocate arrays for the loop
numPoints = Data2(i).H0_Points;
Data2(i).A2_AngleStep = zeros(1, numPoints);
Data2(i).R0_Range = zeros(1, numPoints);
Data2(i).A2_AngleStepTemp = zeros(1, numPoints);
Data2(i).I1_Intensity = zeros(1, numPoints);
factor = Data2(i).H0_SoundSpeed * Data2(i).R0_ScalingFactor / 2;
angleFirst = Data2(i).A2_AngleFirst;
angleScale = Data2(i).A2_ScalingFactor;
intensityScale = Data2(i).I1_ScalingFactor;
% Calculate values in a loop
for j = 1:numPoints
rangeIndex = 2*j + 143;
angleStepIndex = 2*j + 691;
intensityIndex = 2*j + 1211;
Data2(i).R0_Range(j) = factor * hex2dec(dataRow(rangeIndex:rangeIndex+1));
Data2(i).A2_AngleStepTemp(j) = hex2dec(dataRow(angleStepIndex:angleStepIndex+1));
Data2(i).A2_AngleStep(j) = angleFirst + sum(Data2(i).A2_AngleStepTemp(1:j)) * angleScale;
Data2(i).I1_Intensity(j) = intensityScale * hex2dec(dataRow(intensityIndex:intensityIndex+1));
end
end
% Remove temporary field
Data2 = rmfield(Data2, 'A2_AngleStepTemp');
toc
Hope this Helps!

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by