From 6a42574a45cdf966b2a792170fcfd556bce8d484 Mon Sep 17 00:00:00 2001 From: "mhsatman@gmail.com" Date: Thu, 23 May 2024 22:55:08 +0300 Subject: [PATCH] reduce memory allocations in lms() and lts() --- CHANGELOG.md | 1 + src/lms.jl | 18 +++++++++++------- src/lts.jl | 7 ++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f851895..b0bb30d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - More explicit return types, drop `Dict` with `Dict{String, Any}` or `Dict{String, Vector}` - Add `Julia v.1.10` to GitHub actions +- Reduce memory allocation in `lts()` and `lms()` # v0.11.3 diff --git a/src/lms.jl b/src/lms.jl index a46c2e7..d2d1bc0 100644 --- a/src/lms.jl +++ b/src/lms.jl @@ -68,22 +68,26 @@ function lms(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; iters = not iters = minimum([500 * p, 3000]) end bestobjective = Inf - bestparamaters = [] - bestres = [] + bestparamaters = zeros(Float64, p) + bestres = zeros(Float64, n) + origres = zeros(Float64, n) indices = collect(1:n) kindices = collect(p:n) + betas = zeros(Float64, p) + res = zeros(Float64, n) + for _ = 1:iters try k = rand(kindices, 1)[1] sampledindices = sample(indices, k, replace = false) - betas = X[sampledindices, :] \ y[sampledindices] - origres = y .- X * betas - res = sort(origres .^ 2.0) + betas .= X[sampledindices, :] \ y[sampledindices] + origres .= y .- X * betas + res .= sort(origres .^ 2.0) m2 = res[h] if m2 < bestobjective - bestparamaters = betas + bestparamaters .= betas bestobjective = m2 - bestres = origres + bestres .= origres end catch e @warn e diff --git a/src/lts.jl b/src/lts.jl index 615fba9..7dfe379 100644 --- a/src/lts.jl +++ b/src/lts.jl @@ -150,16 +150,17 @@ function lts(X::AbstractMatrix{Float64}, y::AbstractVector{Float64}; iters=nothi allindices = collect(1:n) bestobjective = Inf - besthsubset = [] + besthsubset = zeros(Int, h) + subsetindices = zeros(Int, p) bestobjectiveunchanged = 0 for _ = 1:iters - subsetindices = sample(allindices, p, replace=false) + subsetindices .= sample(allindices, p, replace=false) objective, hsubsetindices = iterateCSteps(X, y, subsetindices, h) if objective < bestobjective bestobjective = objective - besthsubset = hsubsetindices + besthsubset .= hsubsetindices bestobjectiveunchanged = 0 else bestobjectiveunchanged += 1