Skip to content

diff allocate when doing reduction #371

@Beforerr

Description

@Beforerr

I am trying to create a lazy array to do some calculations related to time derivatives, but when reducing the results using sum for example, it allocates a lot. Maybe this is not LazyArrays problem, but I would like to know if there is something I am doing wrong.

using LazyArrays, Unitful, Dates

_diff(x) = diff(x)

LazyArrays.applied_axes(::typeof(_diff), x) = (Base.OneTo(last(Base.axes(x, 1)) - 1),)
LazyArrays.applied_size(::typeof(_diff), x) = (size(x, 1) - 1,)
LazyArrays.applied_eltype(::typeof(_diff), x::AbstractArray{T}) where {T} = Base.promote_op(-, T, T)
LazyArrays.applied_ndims(::typeof(_diff), x) = ndims(x)

function tdiff(data, times; dims=1)
    d1 = Diff(data; dims)
    d2 = ApplyArray(_diff, times)
    return @~ d1 ./ d2
end

data=rand(2000,3) * u"nT"
times = DateTime(2000):Hour(1):DateTime(2000)+Hour(1999)
ddt = tdiff(data, times)
@b sum(ddt)

9.237 ms (17997 allocs: 91.873 MiB, 44.34% gc time)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions