I came across a discussion post on using [CartesianIndex()]
for singleton insertion in Julia arrays. Intrigued by potential performance differences, I conducted a benchmark comparing it with the reshape
method.
Benchmark code:
(M, N, P, K) = (20, 30, 40, 50)
na = [CartesianIndex()]
A = randn(M, N, P)
B = randn(M, N, K)
@time C = A .* B[:,:,na,:]
@time D = A .* reshape(B,M,N,1,K)
Results:
0.001471 seconds (50 allocations: 9.385 MiB)
0.001449 seconds (8 allocations: 9.156 MiB)
The reshape approach seems slightly faster, and Iām seeking insights into the reasons behind this performance difference. Any explanations or thoughts are welcome.