@dpsanders , yep!
code:
using ForwardDiff
function f(x::Vector{T}) where T
x2 = x .* x
return log(sum(x2))
end
v1 = [1., 2.]
v2 = [1., 2., 3.]
@time ForwardDiff.hessian(f, v1)
@time ForwardDiff.hessian(f, v1)
@time ForwardDiff.hessian(f, v2)
@time ForwardDiff.hessian(f, v2)
out:
julia> @time ForwardDiff.hessian(f, v1)
2.000931 seconds (4.09 M allocations: 232.179 MiB, 99.94% compilation time)
2×2 Matrix{Float64}:
0.24 -0.32
-0.32 -0.24
julia>
julia> @time ForwardDiff.hessian(f, v1)
0.000030 seconds (10 allocations: 1.359 KiB)
2×2 Matrix{Float64}:
0.24 -0.32
-0.32 -0.24
julia>
julia> @time ForwardDiff.hessian(f, v2)
2.122246 seconds (4.02 M allocations: 224.059 MiB, 7.06% gc time, 99.97% compilation time)
3×3 Matrix{Float64}:
0.122449 -0.0408163 -0.0612245
-0.0408163 0.0612245 -0.122449
-0.0612245 -0.122449 -0.0408163
julia>
julia> @time ForwardDiff.hessian(f, v2)
0.000030 seconds (10 allocations: 2.828 KiB)
3×3 Matrix{Float64}:
0.122449 -0.0408163 -0.0612245
-0.0408163 0.0612245 -0.122449
-0.0612245 -0.122449 -0.0408163
julia>
first call f(x) with v2 - 2.122246 seconds