Skip to content

Commit

Permalink
Performance improvement to iterate of StepRange{<:TimeType} (#29404)
Browse files Browse the repository at this point in the history
  • Loading branch information
kcajf authored and KristofferC committed Oct 18, 2018
1 parent f878850 commit 5f2759d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion stdlib/Dates/src/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function in(x::T, r::StepRange{T}) where T<:TimeType
n >= 1 && n <= length(r) && r[n] == x
end

Base.iterate(r::StepRange{<:TimeType}, i::Int=0) = length(r) <= i ? nothing : (r.start + r.step*i, i + 1)
Base.iterate(r::StepRange{<:TimeType}) = length(r) <= 0 ? nothing : (r.start, (length(r), 1))
Base.iterate(r::StepRange{<:TimeType}, (l, i)) = l <= i ? nothing : (r.start + r.step * i, (l, i + 1))

+(x::Period, r::AbstractRange{<:TimeType}) = (x + first(r)):step(r):(x + last(r))
+(r::AbstractRange{<:TimeType}, x::Period) = x + r
Expand Down
8 changes: 4 additions & 4 deletions stdlib/Dates/test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let
@test maximum(dr) == last(dr)
@test dr[1] == f
@test dr[end] <= l
@test iterate(dr) == (first(dr), 1)
@test iterate(dr) == (first(dr), (length(dr), 1))

if len < 10000
dr1 = [i for i in dr]
Expand Down Expand Up @@ -113,7 +113,7 @@ let
@test maximum(dr) == first(dr)
@test dr[1] == l
@test dr[end] >= f
@test iterate(dr) == (first(dr), 1)
@test iterate(dr) == (first(dr), (length(dr), 1))

if len < 10000
dr1 = [i for i in dr]
Expand Down Expand Up @@ -173,7 +173,7 @@ let
@test maximum(dr) == last(dr)
@test dr[1] == f
@test dr[end] <= l
@test iterate(dr) == (first(dr), 1)
@test iterate(dr) == (first(dr), (length(dr), 1))

if len < 10000
dr1 = [i for i in dr]
Expand Down Expand Up @@ -231,7 +231,7 @@ let
@test maximum(dr) == first(dr)
@test dr[1] == l
@test dr[end] >= f
@test iterate(dr) == (first(dr), 1)
@test iterate(dr) == (first(dr), (length(dr), 1))

if len < 10000
dr1 = [i for i in dr]
Expand Down

0 comments on commit 5f2759d

Please sign in to comment.