Skip to content

Instantly share code, notes, and snippets.

@fredrikekre
Last active April 19, 2016 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrikekre/2c703d43102eda4a9354044c34b1cc30 to your computer and use it in GitHub Desktop.
Save fredrikekre/2c703d43102eda4a9354044c34b1cc30 to your computer and use it in GitHub Desktop.

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