Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying array columns

Tags:

julia

I have an algorithm that requires one column of an array to be replaced by another column of the same array. I tried doing it with slices, and element-wise.

const M = 10^4
const N = 10^4
A = rand(Float32, M, N)
B = rand(Float32, N, M)

function copy_col!(A::Array{Float32,2},col1::Int,col2::Int)
  A[1:end,col2] = A[1:end,col1]
end

function copy_col2!(A::Array{Float32,2},col1::Int,col2::Int)
  for i in 1:size(A,1)
    A[i,col2] = A[i,col1]
  end
end

[Both functions+rand are called here once for compilation]

@time (for i in 1:20000 copy_col!(B, rand(1:size(B,2)),rand(1:size(B,2)) ); end )
@time (for i in 1:20000 copy_col2!(B, rand(1:size(B,2)),rand(1:size(B,2)) ); end )

>>  0.607899 seconds (314.81 k allocations: 769.879 MB, 25.05% gc time)
>>  0.213387 seconds (117.96 k allocations: 2.410 MB)

Why does copying using slices perform so much worse? Is there a better way than what copy_col2! does?

like image 1000
skleinbo Avatar asked Mar 13 '23 19:03

skleinbo


1 Answers

A[1:end,col1] makes a copy of indexed column first then it copies over to A[1:end,col2] so copy_col! allocates more and runs longer. There are sub, slice, and view that may remedy allocations in this case.

like image 104
Lutfullah Tomak Avatar answered Mar 19 '23 02:03

Lutfullah Tomak