Optimise function with datetime operations
Show older comments
Hi I have a function which is doing simple operations in a large file. I ran the profiler and it seems like most the time has been spent in datetime operaitons. See code below:
function batterystatus = freqdataV3( freq, date,FFRExcWindow1, FFRExcWindow2, REDstart, REDend, batpower, batenergy, batinitial , batefficiency)
Energy0 = batenergy * batinitial;
uplimit = 50.015;
lowlimit = 49.985;
statuplimit = 50.5;
statlowlimit = 49.5;
batterystatus(:,7) = freq;
batterystatus(1 ,1) = Energy0;
batterystatus(1, 2) = 0;
batterystatus(1, 3) = 0;
batterystatus(1, 4) = 0;
batterystatus(1, 5) = 0;
batterystatus(1, 6) = 0;
FFRfinishdate = datetime(year(date), month(date), day(date),floor(FFRExcWindow1), rem(FFRExcWindow1, 1) * 60, 0);
FFRstartdate = datetime(year(date), month(date), day(date),floor(FFRExcWindow2), rem(FFRExcWindow2, 1) * 60, 0);
REDstartdate = datetime(year(date), month(date), day(date), floor(REDstart), rem(REDstart, 1) * 60,0);
REDenddate = datetime(year(date), month(date), day(date), floor(REDend), rem(REDend, 1) * 60,0);
for i = 2:numel(freq)
if weekday(date(i)) >= 2 && weekday(date(i)) <= 6 && ge(date(i),FFRfinishdate(i)) && lt(date(i),REDstartdate(i))
if batterystatus(i - 1, 1) >= (batenergy - 1)
batterystatus(i ,1:6 ) = batterystatus(i -1 ,1:6 );
else
(...)
When I run the profiler, it highlights the "if weekday(date(i))..." in RED. The whole function has a few more "if"s below, but they similar to the one above but with different conditions. The profiler shows that almost half the total time is due to datetime.subsref. Is this just to perform datetime operations such as "ge" and "lt"? If so, is there any clever away around it that would save me time to run it?

Thanks in advance.
Accepted Answer
More Answers (0)
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!