I’d recommend a struct of arrays for that use case. Being lazy and just using an Array (pretend column 1 is x
, column 2 is y
, and column 3 is z
):
function f4!(A)
@inbounds @simd for i in 1:size(A,1)
x = A[i,1]; y = A[i,2]; z = A[i,3]
A[i,1] = 2x; A[i,2] = x*y; A[i,3] = x*z
end
end
Benchmarking:
julia> A = rand(1000,3);
julia> @btime f1!($v1)
931.406 ns (0 allocations: 0 bytes)
julia> @btime f2!($v2)
58.993 μs (3000 allocations: 46.88 KiB)
julia> @btime f3!($v2)
1.489 μs (0 allocations: 0 bytes)
julia> @btime f4!($A)
315.979 ns (0 allocations: 0 bytes)
julia> versioninfo()
Julia Version 1.3.1-pre.0
Commit b42f4ab* (2019-11-26 17:58 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.1 (ORCJIT, haswell)
Environment:
JULIA_NUM_THREADS = 24