Hi,
I have a square matrix M, and I would like to update it in-place such that the kth column is multiplied by some scalar s. I’ve tried implementing it with dot syntax and simple rolling out of the for loop.
function diagright1!(M, s, col)
@views M[:, col] .*= s
end
function diagright2!(M, s, col)
d = size(M, 1)
for ind in 1:d
M[ind, col] = M[ind, col] * s
end
end
I was hoping the dot syntax will be as performant as the for loop, but I got this benchmark result instead.
M = rand(500, 500);
s = 3.2
col = 2
b1 = @benchmarkable diagright1!(M1, $s, $col) setup = (M1 = copy($M))
b2 = @benchmarkable diagright2!(M2, $s, $col) setup = (M2 = copy($M))
run(b1)
run(b2)
BenchmarkTools.Trial:
memory estimate: 96 bytes
allocs estimate: 2
--------------
minimum time: 277.000 ns (0.00% GC)
median time: 327.000 ns (0.00% GC)
mean time: 346.747 ns (0.00% GC)
maximum time: 17.267 μs (0.00% GC)
--------------
samples: 10000
evals/sample: 1
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 181.000 ns (0.00% GC)
median time: 198.000 ns (0.00% GC)
mean time: 203.403 ns (0.00% GC)
maximum time: 13.936 μs (0.00% GC)
--------------
samples: 10000
evals/sample: 1
Dot syntax is >50% slower than the naive for loop implementation. I’ve tried removing @views macro and/or removing the . from .*=, but the above implementation was the fastest among the bunch.
My question is, Is there some clever trick I missed that speeds up the dot syntax implementation, or is this performance to be expected and thus unavoidable?