Is there a way to optimize this?

3 vues (au cours des 30 derniers jours)
Jonathan Pelletier-Marcotte
Réponse apportée : Deepak le 28 Août 2024
I'd like to speed up this code but I don't know how to improve it more, is there a way to optimize it?
I need to build an array with every Status On for every minute of the day.
Date = datetime(2018,11,08);
Time_Ref = Date+minutes(1):minutes(1):Date+days(1);
tres = cell(length(Time_Ref),1);
Time_on = {'2018-11-08 00:00:00';'2018-11-08 00:00:00';'2018-11-08 00:00:00';'2018-11-08 00:46:29';...
'2018-11-08 03:51:33';'2018-11-08 08:23:40';'2018-11-08 23:51:37'};
Time_off = {'2018-11-09 00:00:00';'2018-11-09 00:00:00';'2018-11-09 00:00:00';'2018-11-08 12:46:41';...
'2018-11-08 03:52:44';'2018-11-08 08:23:50';'2018-11-08 23:51:47'};
Status = {850;3030;6042;6052;7018;6052;6052};
tic
for j=1:length(Time_Ref)
n=1;
while true
if Time_off(n)> Time_Ref(j)-seconds(59) && Time_on(n)<Time_Ref(j)
tres{j} = [tres{j},Status(n)];
end
if n >= length(Time_on) || Time_on(n) > Time_Ref(j)
break;
end
n=n+1;
end
end

Réponses (1)

Deepak
Deepak le 28 Août 2024
Hi @Jonathan Pelletier-Marcotte, from my understanding, you have generated the “tres” array that stores the status codes for each minute of the day, based on when each status is active. You have built the “Time_Ref,” “Time_on,” and “Time_off” arrays, which serve as input to the for loop. Now, you want to optimize the code further to get the same result.
To do this, we can convert “Time_on” and “Time_off” to datetime objects once to avoid repeated conversions inside the loop. Next, we can pre-allocate the “tres” array with empty arrays, which improves the performance. Finally, we can use logical indexing inside the for loop to avoid nested loops that improves the performance further.
Below is the updated MATLAB code with the described changes:
Date = datetime(2018, 11, 08);
Time_Ref = Date + minutes(1):minutes(1):Date + days(1);
tres = cell(length(Time_Ref), 1);
% Convert Time_on and Time_off to datetime arrays
Time_on = datetime({'2018-11-08 00:00:00'; '2018-11-08 00:00:00'; '2018-11-08 00:00:00'; '2018-11-08 00:46:29'; ...
'2018-11-08 03:51:33'; '2018-11-08 08:23:40'; '2018-11-08 23:51:37'});
Time_off = datetime({'2018-11-09 00:00:00'; '2018-11-09 00:00:00'; '2018-11-09 00:00:00'; '2018-11-08 12:46:41'; ...
'2018-11-08 03:52:44'; '2018-11-08 08:23:50'; '2018-11-08 23:51:47'});
Status = [850; 3030; 6042; 6052; 7018; 6052; 6052];
tic
for j = 1:length(Time_Ref)
% Find indices where the condition is met
indices = Time_off > Time_Ref(j) - seconds(59) & Time_on < Time_Ref(j);
% Collect statuses for those indices
tres{j} = Status(indices)';
end
toc
Elapsed time is 0.082180 seconds.
Attaching the documentation for logical indexing in MATLAB for reference:
I hope this helps.

Catégories

En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by