Optimise function with datetime operations
이전 댓글 표시
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!