HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

90 year data simulation in need of performance increase

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
yearsimulationneedperformanceincreasedata

Problem

I developed some code for my masters project that will simulate 90 years daily data using 1000 different data sets. The code is working fine and gives the correct output that I wanted but the processing time are very high. It took about 8 hours to finish the simulation.

` tic
%% importing the csv file with selected column
files=dir('_scen_.csv');
for i=1:length(files);
LHR=importcsv(files(i).name);

%% Definable variables
% Define These Value
TAW=-216; %total available water
RAW=-129; %readily available water
KC=1.0; %crop coefficient
IRL=15; %intense rain level
RC=(80/100); %percentage of recharge
RO=(1-RC); %percentage of runoff

% The very first row of Soil Moisture Deficit
for j=1
SMD(j,i)=(LHR.RAIN(j)-LHR.PET(j));
if SMD(j,i)>0;
SMD(j,i)=0;
elseif SMD(j,i)0;
SMD(k,i)=0;
elseif SMD(k,i)(LHR.RAIN(n)-AET(n,i));
HER(n,i)=0;
end
end
%% Calculation of recharge anf runoff
for o=1:(length(HER));
if (HER(o,i)+(abs(TAW)-SMD(o,i)))abs(TAW);
if HER(o,i)>IRL;
RUNOFF(o,i)=RO*HER(o,i);
elseif HER(o,i)IRL;
RECHARGE(o,i)=RC*HER(o,i);
elseif HER(o,i)

Is there any improvement scope for this code that might reduce the processing time? Sorry if the code looks unprofessional; I am in the beginner stage for MATLAB programming.

Solution

This cleanup code at the end is indicative of the problem with this program:

clear i
clear j
clear k
clear l
clear m
clear n
clear o
clear p


Nobody is going to reverse-engineer your minified / obfuscated code to understand it. You will probably have a hard time understanding it yourself if you come back to it after a few weeks. The program is therefore unmaintainable.

I wouldn't have much confidence in the correctness of the results, either. The code should be broken down into functions, each of which has a single purpose, specific inputs and outputs, and is separately testable.

Code Snippets

clear i
clear j
clear k
clear l
clear m
clear n
clear o
clear p

Context

StackExchange Code Review Q#59282, answer score: 10

Revisions (0)

No revisions yet.