From ac2b29da0c2092cc9ff5274c66b76b152211852a Mon Sep 17 00:00:00 2001 From: Daniel Thom Date: Wed, 30 Oct 2024 15:52:29 -0600 Subject: [PATCH] Fix peformance issue with get_resolution The code was unnecessarily checking the consistency of a single time series resolution in calls to get_time_series. The check occurs when the array is added to the system. --- src/single_time_series.jl | 13 +------------ src/utils/utils.jl | 12 +++++++++--- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/single_time_series.jl b/src/single_time_series.jl index 5c92bed4b..9cbf84b5a 100644 --- a/src/single_time_series.jl +++ b/src/single_time_series.jl @@ -44,7 +44,7 @@ function SingleTimeSeries(; return SingleTimeSeries( name, data, - _get_resolution(data), + get_resolution(data), scaling_factor_multiplier, internal, ) @@ -72,17 +72,6 @@ function SingleTimeSeries( ) end -function _get_resolution(data::TimeSeries.TimeArray) - if length(data) < 2 - throw( - ConflictingInputsError( - "Resolution can't be inferred from the data. Please select an appropiate constructor.", - ), - ) - end - return TimeSeries.timestamp(data)[2] - TimeSeries.timestamp(data)[1] -end - """ Construct SingleTimeSeries from a TimeArray or DataFrame. diff --git a/src/utils/utils.jl b/src/utils/utils.jl index ebc424aba..e82d8a839 100644 --- a/src/utils/utils.jl +++ b/src/utils/utils.jl @@ -384,12 +384,18 @@ end """ Return the resolution from a TimeArray. """ -function get_resolution(ts::TimeSeries.TimeArray) +function get_resolution(ts::TimeSeries.TimeArray; check_consistency = false) + if length(ts) < 2 + throw(ConflictingInputsError("Resolution can't be inferred from the data.")) + end + + if !check_consistency + return TimeSeries.timestamp(ts)[2] - TimeSeries.timestamp(ts)[1] + end + tstamps = TimeSeries.timestamp(ts) timediffs = unique([tstamps[ix] - tstamps[ix - 1] for ix in 2:length(tstamps)]) - res = [] - for timediff in timediffs if mod(timediff, Dates.Millisecond(Dates.Day(1))) == Dates.Millisecond(0) push!(res, Dates.Day(timediff / Dates.Millisecond(Dates.Day(1))))