Skip to content

getindex cost memory #324

@kongdd

Description

@kongdd
using StructArrays
using BenchmarkTools

abstract type AbstractSoilParam{T} end

@kwdef mutable struct ParamCampbell{T} <: AbstractSoilParam{T}
  θ_sat::T = 0.287       # [m3 m-3]
  # θ_res::T = 0.075     # [m3 m-3]
  ψ_sat::T = -10.0       # [cm]
  Ksat::T = 34 / 3600    # [cm s-1]
  b::T = 4.0             # [-]
end

mutable struct SoilParam{T}
  par::StructVector{ParamCampbell{T}} # multiple layers
end

begin
  N = 10
  θ_sat = fill(0.287, N)       # [m3 m-3]
  ψ_sat = fill(-10.0, N)       # [cm]
  Ksat = fill(34 / 3600, N)    # [cm s-1]
  b = fill(4.0, N)
  par = StructVector{ParamCampbell{Float64}}((θ_sat, ψ_sat, Ksat, b))
  param = SoilParam{Float64}(par)
end

function test_param(param::SoilParam{Float64}, N::Int)
  @inbounds for k in 1:100_00
    for i in 1:N
      _par = param.par[i]
    end
  end
end
julia> @btime test_param($param, N)
  2.558 ms (100000 allocations: 4.58 MiB)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions