Home > Net >  Simple calculation for all combination (brute force) of elements in two arrays, for better performan
Simple calculation for all combination (brute force) of elements in two arrays, for better performan

Time:10-07

I am new to Julia (some experience with Python). The main reason I am starting to use Julia is better performance for large scale data processing.

I want to get differences of values (int) of all possible combinations in two arrays.

Say I have two arrays.

a = [5,4]
b = [2,1,3]

Then I want to have differences of all combinations like a[1] - b[1], a[1] - b[2] ..... a[3] - b[1], a[3] - b[2]

The result will be 3 x2 array [3 2; 4 3; 2 1]

Then something I came up is

a = [5,4]  
b = [2,1,3]
diff_matrix = zeros(Int8, size(b)[1], size(a)[1])
for ia in eachindex(a)
    for ib in eachindex(b)
        diff_matrix[ib,ia]= a[ia] - b[ib]
    end
end
println(diff_matrix)

It works but it uses iteration inside of iteration and I assume the performance will not be great. In real application, length of array will be long (like a few hundreds), and this process needs to be done for millions of combinations of arrays.

Is there any better (better performance, simpler code) approach for this task ?

CodePudding user response:

If you wrapped the code in a function your code would be already reasonably fast.

This is exactly the power of Julia that loops are fast. The only thing you need to avoid is using global variables in computations (as they lead to code that is not type stable).

I write the code would be "reasonably fast", as it could be made faster by some low-level tricks. However, in this case you could just write:

julia> a = [5,4]                
2-element Vector{Int64}:        
 5                              
 4                              
                                
julia> b = [2,1,3]              
3-element Vector{Int64}:        
 2                              
 1                              
 3                              
                                
julia> permutedims(a) .- b      
3×2 Matrix{Int64}:              
 3  2                           
 4  3                           
 2  1                           

and this code will be fast (and much simpler as a bonus).

  • Related