Skip to content

Instantly share code, notes, and snippets.

@fredrikekre fredrikekre/gandke.md
Last active Apr 19, 2016

Embed
What would you like to do?

Calculating g and ke at the same time

using ForwardDiff
# element function
function ele{T,Q}(x::Vector{T},y::Vector{Q})
    return x*dot(x,y)
end

ele_closure{T}(x::Vector{T}) = ele(x,y);
x = rand(4); y = rand(4); ntimes = 10e5;

Why is this slower

cache = ForwardDiffCache()
kout = zeros(4,4)
k = copy(kout)
g = zeros(4)
tic()
for i = 1:ntimes
    k, allres = ForwardDiff.jacobian!(kout,ele_closure,x,ForwardDiff.AllResults,cache=cache)
    value!(g,allres)
end
toc()


    elapsed time: 4.254816853 seconds

than this?

tic()
# residual
for i = 1:ntimes
    g = ele(x,y)
end

cache = ForwardDiffCache()
for i = 1:ntimes
    kfunc = ForwardDiff.jacobian(ele_closure,x,cache=cache)
end
toc()


    elapsed time: 3.654483469 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.