Hello,
I’m not sure how to optimize the following for loop.
array_1 and array_2 are two Vector{Vector{Int64}}s, and I’m interested in evaluating which combinations (of 2 elements) of their vector components has an element-wise sum equal to a specified sum_target, which of course is a Vector{Int64}.
The code I’m currently using is the following:
using BenchmarkTools
function f()
n_elements = 50
array_1 = [Int64.(rand(Int8,500)) for i in 1:n_elements]
array_2 = [Int64.(rand(Int8,500)) for i in 1:n_elements]
sum_target = Int64.(rand(Int8,500))
solutions = Vector{Vector{Int64}}[]
iterator_over_combinations = Iterators.product(array_1,array_2)
for combination in iterator_over_combinations
if all(sum(combination) .== sum_target)
push!(solutions, combination )
end
end
return solutions
end
The number of vector elements of array_1 and array_2 is here set to n_elements::Int64 = 50 in order for @btime to work, but in the real application I have like n_elements::Int64 = 10^11 and combine ten arrays instead of just two.
Right now I get:
julia> @btime f() ;
2.587 ms (10205 allocations: 21.02 MiB)
Thank you very much in advance.