Operations on SubArrays scale quite poorly with size (or to be precise, they scale linearly with size which is quite normal, but given that getindex is almost constant-time, one might hope that views could also be optimised?):
using ReverseDiff, Chairmarks
function f_getindex(x)
b = x[2:end]
return sum(b)
end
function f_view(x)
b = @view x[2:end]
return sum(b)
end
for d in [10, 20, 50, 100]
x = randn(d)
tape_gi = ReverseDiff.GradientTape(f_getindex, x)
tape_v = ReverseDiff.GradientTape(f_view, x)
result = similar(x)
println("d = $d:")
print(" getindex: "); display(@b ReverseDiff.gradient!($result, $tape_gi, $x))
print(" view: "); display(@b ReverseDiff.gradient!($result, $tape_v, $x))
end
d = 10:
getindex: 146.648 ns
view: 284.160 ns
d = 20:
getindex: 151.278 ns
view: 512.643 ns
d = 50:
getindex: 153.263 ns
view: 1.188 μs
d = 100:
getindex: 182.759 ns
view: 2.306 μs
(ppl) pkg> st
Status `~/ppl/Project.toml`
[0ca39b1e] Chairmarks v1.3.1
[37e2e3b7] ReverseDiff v1.16.2
julia> versioninfo()
Julia Version 1.11.9
Commit 53a02c0720c (2026-02-06 00:27 UTC)
Build Info:
Official https://julialang.org/ release
Platform Info:
OS: macOS (arm64-apple-darwin24.0.0)
CPU: 10 × Apple M1 Pro
WORD_SIZE: 64
LLVM: libLLVM-16.0.6 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Operations on
SubArrays scale quite poorly with size (or to be precise, they scale linearly with size which is quite normal, but given thatgetindexis almost constant-time, one might hope thatviews could also be optimised?):