Closure vs call overloading


I have defined a function F that takes a function fas an argument (say F solves for x such that f(x) = 0). The function F calls the function f within large for loops etc.

I have noticed that there was a large difference in performances (3x) between having f, the first argument of F, as a closure VS a callable object. I thought that, in Julia 0.5, closures were as performant as callable objects. Unfortunately, I cannot find a simple example that reproduces the problem. I think the difference is due to the fact f is not inlined within a call F. Would someone know more? Is there a way to force inlining of f within F?

This code clarifies the two ways of defining f:

type Parameters
p = Parameters(10.0, 10.0)

# closure
function f(p::Parameters, x)
F(x -> f(p, x))

# call overloading
function (p::Parameters)(x)



It looks like you are using global variables. If you make p const, or wrap everything in a function, then they should be equally performant.