Thanks, makes sense.
I needed the same batch of matrices multiple times, hence I decided to store and reuse them. Otherwise, for one-time use, constructing them on the fly would be ideal.
Also, StaticArrays looks like the way to go.
using MKL
using LinearAlgebra
using StaticArrays
using BenchmarkTools
N = 10000000;
aar = range(0,0.1,N);
bar = range(0,0.05,N);
function matop(a, b)
return a .* Array([b a; a b]);
end
function basic(a, b)
c = zeros(Float64,2,2,length(a));
for hi = 1:length(a)
c[:,:,hi] = matop(a[hi],b[hi]);
end
return c
end
@btime car = basic($aar, $bar);
function broadcast1(a, b)
c = zeros(Float64,2,2,length(a));
broadcast!(matop, eachslice(c, dims=3), a, b);
return c
end
@btime car1 = broadcast1($aar, $bar);
function broadcast2(a, b)
c = stack(matop.(a, b), dims=3);
return c
end
@btime car2 = broadcast2($aar, $bar);
function matops(a, b)
return a * @SMatrix[b a; a b];
end
function broadcast3(a, b)
c = matops.(a, b);
return c
end
@btime car3s = broadcast3($aar, $bar);
car = basic(aar, bar);
car1 = broadcast1(aar, bar);
car2 = broadcast2(aar, bar);
car3s = broadcast3(aar, bar);
car3 = zeros(Float64,2,2,N);
for hi = 1:N
car3[:,:,hi] = car3s[hi][1:2,1:2];
end
carâcar1
carâcar2
carâcar3
3.048 s (100000003 allocations: 4.02 GiB)
2.923 s (100000003 allocations: 4.02 GiB)
4.251 s (100000006 allocations: 4.10 GiB)
75.003 ms (3 allocations: 305.18 MiB)