Skip to content

Commit

Permalink
Fix peformance issue with get_resolution
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
daniel-thom committed Oct 30, 2024
1 parent 8c4d57d commit ac2b29d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
13 changes: 1 addition & 12 deletions src/single_time_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function SingleTimeSeries(;
return SingleTimeSeries(
name,
data,
_get_resolution(data),
get_resolution(data),
scaling_factor_multiplier,
internal,
)
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions src/utils/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down

0 comments on commit ac2b29d

Please sign in to comment.