# How to call a function that returns a function efficiently

**URL:** https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851
**Category:** General Usage
**Created:** [June 21, 2018, 11:39am UTC](https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851 "2018-06-21T11:39:21Z")
**Posts on this page:** 5
**Page:** 2

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [June 24, 2018, 2:05pm UTC](https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851/22 "2018-06-24T14:05:53Z")

</div>

Instead of the QuantEcon interpolation, please try out Interpolations.jl. To do this, first add in the library, and a convenience function (hopefully unnecessary if a recent PR is merged)

```julia
using Interpolations
function (itp::Interpolations.GriddedInterpolation{T,N,TCoefs,IT,K,pad})(args...)where {T,N,TCoefs,IT,K,pad}
    itp[args...]
end

```

Then you should be able to replace the following

> [@drarnau](#):
>
> aux\_inter = LinInterp(f.a\_values, aux\_exp)

With something like

```julia
aux_inter = interpolate((f.a_values,), aux_exp, Gridded(Linear()))

```

Please tell us if there is a performance difference.

---

<div class="post-metadata">

### Author: ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)
#### Post date: [June 24, 2018, 3:16pm UTC](https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851/23 "2018-06-24T15:16:49Z")

</div>

Thank for your suggestion, Jesse. I’ve run the whole code with your proposed change and I get no speed time gain. Actually, it seems slightly slower, around 32 minutes.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 24, 2018, 3:54pm UTC](https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851/24 "2018-06-24T15:54:47Z")

</div>

@drarnau are you using `ifort` or `gfortran`? Can you share the Fortran code? There aren’t any technical details making this difference insurmountable (otherwise that would be a bug to report), but there definitely is some unexplained difference to try and find here.

My guess is that something is very off here. For example, you’re using a bunch of heap allocated arrays and I see a bunch of manual `copy` operations. `ifort` will sometimes stack-allocate those, in which case the right comparison will be to use StaticArrays.jl and this could account for the timing difference (I haven’t profiled those to see where your issue truly lies though). For reference on `ifort`’s allocation behavior:

> **[Stack Overflows in Fortran](https://jblevins.org/log/segfault)**
>
> Some stack overflows can be avoided by allocating large automatic arrays on the heap.

---

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [June 24, 2018, 3:56pm UTC](https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851/25 "2018-06-24T15:56:00Z")

</div>

The other thing is to play around with a different root finder to replace the golden method. Since your calculations are so expensive, things that will decrease the number may be very helpful.

I would try [https://github.com/JuliaMath/Roots.jl/blob/master/README.md](https://github.com/JuliaMath/Roots.jl/blob/master/README.md) with a few variations on the derivative free methods

---

<div class="post-metadata">

### Author: ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)
#### Post date: [June 24, 2018, 4:52pm UTC](https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851/26 "2018-06-24T16:52:47Z")

</div>

So far, I’m just trying to write a Julia version of [this code](https://www.aeaweb.org/content/file?id=5684). The file I’m linking is from a published paper and contains more stuff than what I’m trying to replicate. The program consists of many files. What I’m trying to replicate is in a folder called ss\_codes. I compiled it both in gfortran and ifort and it was much faster than my Julia version. Actually, my Julia version it’s just a part of that program (the computationally intensive part).

What is the difference of using StaticArrays.jl with respect to what I’m using now? Could that improve the performance?

Thanks for your comment!

[Previous page](https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851.md?page=1)
