Hi, I was wondering if there is a better way of writing the function add_and_mul below, which is effectively (up to some scaling and addition) trying to compute the dot-product between the various row-vectors in two matrices? There must be a more elegant way of doing this, but I’m not yet seeing it…
using BenchmarkTools, Random, LinearAlgebra
Random.seed!(42)
function add_and_mul(X1, A, r, s, d)
X2 = Matrix{Float64}(undef, size(X1))
X2[:, 1:d] = view(X1, :, 1:d) * s
X2[:, d+1] = view(X1, :, d+1) * r + sum(A .* (view(X2, :, 1:d) - view(X1, :, 1:d)*r), dims = 2)
return X2
end
function RunBench(N, d)
r = rand()
s = rand()
X1 = rand(N, d+1)
A = rand(N, d)
display(add_and_mul(X1, A, r, s, d))
display(@benchmark add_and_mul($X1, $A, $r, $s, $d))
end
RunBench(10_000, 5)
which gives
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min … max): 77.917 μs … 19.387 ms ┊ GC (min … max): 0.00% … 98.92%
Time (median): 191.792 μs ┊ GC (median): 0.00%
Time (mean ± σ): 293.755 μs ± 542.751 μs ┊ GC (mean ± σ): 32.63% ± 19.26%
▁▃█▇▄▂ ▁ ▂
█████████▆▄▃▁▁▁▁▁▄▃▃▁▃▁▁▄▅▅▄▆▅▆▅▆▆▆▇▆▇▆▇▇▇▇▆▆▇▆▆▇▆▆▅▆▆▅▆▆▆▅▅▅ █
77.9 μs Histogram: log(frequency) by time 2.46 ms <
Memory estimate: 2.21 MiB, allocs estimate: 24.