Both . (dot) operations allocate new arrays. The second one can be completely avoided, using,
for example*:
all(>=(0), md.cap[i] - neededResource[i] for i in eachindex(md.cap, neededResource))
(using both arrays in eachindex will guarantee that the operation is inbounds, but you could in put only one of them for simplicity)
The first one is allocating a new array neededResource, and the only way to avoid that is to preallocate it outside the function, to make the function in-place.
* there are alternatives, for example:
all(>=(0), x[1] - x[2] for x in zip(md.cap, neededResource))
Thank you!
I modified the function according to your suggest:
function resourceAvailable(usedResource::Vector{Int32}, neededResource::Vector{Int32}, actResource::Vector{Int32}, capResource::Vector{Int32})
@inbounds for i in eachindex(neededResource, usedResource, actResource)
neededResource[i] = usedResource[i] + actResource[i]
end
return all(>=(0), capResource[i] - neededResource[i] for i in eachindex(capResource, neededResource))
end
Why not combine the predicate and subtraction? The optimizer can probably resolve this, but it seems like messy code to write. But really, why subtract and compare to 0 when you can compare the numbers directly?