I’ve a preallocated vector temp which has the values of (y - y_pred ).^2 where y and y_pred are 1D vectors.
I’ve to reuse that vector in 2 or more different functions, what is the best solution for this. Note: performance matters✏️
eg:
const temp = Vector{Float64}(undef, size(x, 1))
function mse(x::Vector{Float64}, y::Vector{Int}, w::Float64, b::Float64)
for ind in eachindex(y,y_pred)
temp[ind] = (y[ind] - y_pred[ind]) ^ 2 # (y - y_pred)^2
end
end
function batchgd(x::Vector{Float64},y::Vector{Int},w::Float64,b::Float64, lr::Float64, threshold = 0.00001, max_iter = 10000)
for ind in eachindex(y, y_pred) temp[ind] = sqrt(y[ind] - y_pred[ind]) # since we have squared value we do sqrt.(reusing memory) end
end
Here I reuse memory temp with minor change i.e) already I have squared value in temp and so in the function batchgd I do sqrt. so my question is what is the best way to reuse memory.
- Creating a vector globally and calling in wherever required
- computing y - y_pred everytime within each function where needed
- returning the vector from one function and using them in another.
- is there any better way.