# Why the code recompiles?

**URL:** <https://discourse.julialang.org/t/why-the-code-recompiles/31092>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 14, 2019, 5:33pm UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092 "2019-11-14T17:33:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [November 14, 2019, 5:33pm UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092/1 "2019-11-14T17:33:54Z")

</div>

When I am working with scientific projects, I am quite an old school guy who writes code and executes that with `include("source.jl")` in the Julia REPL. That had worked pretty well so far, but today I encountered a recompilation issue which makes my work less pleasant. A code which I execute with Julia 1.3 is:

```julia
using DifferentialEquations

struct PS{T}
   V::T
   VR::T
   r::T
   C::T
   Vext::Function
end

function phaseslip!(dq,q,p,t)
   V,VR,r,C,Vext = p.V, p.VR, p.r, p.C, p.Vext
   qL,qR = q[1],q[2]
   dq[1] = V - sin(qL) - 1/C * (qL - qR) - Vext(t)
   dq[2] = - VR * sin(qR) + r/C * (qL - qR) + r*Vext(t)
end

### Solving the system

p = PS(0.5,0.5,0.5,2.0,t->0.1*sin(t))

q0 = [0.,0.]
tspan = (0.0,2000)

prob = ODEProblem(phaseslip!,q0,tspan,p)
@time sol = solve(prob,dtmax=1.)

```

Every time I run it, I get 2.6 seconds. If I specifically execute the part after “Solving the system” I get 0.01 second. What is the reason for such behaviour? How can I avoid that?

---

<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:** [November 14, 2019, 6:07pm UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092/2 "2019-11-14T18:07:03Z")

</div>

> [@Janis\_Erdmanis](#):
>
> Vext::Function

This will slow your code down BTW. It’s better to parameterize that `::F`

> [@Janis\_Erdmanis](#):
>
> Every time I run it, I get 2.6 seconds. If I specifically execute the part after “Solving the system” I get 0.01 second. What is the reason for such behaviour? How can I avoid that?

The using for DifferentialEquations.jl is probably a chunk of that. If you re-run with the REPL window still open it won’t have to keep `using`.

---

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [November 14, 2019, 6:24pm UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092/3 "2019-11-14T18:24:41Z")

</div>

When I comment out `phaseslip!` for repeated run it runs fast. `using DifferentialEquations` does not affect the rerun time in the REPL.

I will check if parallelization of F helps. But I really would prefer to keep `Vext` as that makes the system conceptually simple and general.

---

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [November 14, 2019, 6:37pm UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092/4 "2019-11-14T18:37:43Z")

</div>

The parametrization of `Vext` did not help. The following code has the same problem:

```julia
using DifferentialEquations

struct PS{T}
    V::T
    VR::T
    r::T
    C::T
end

function phaseslip!(dq,q,p,t)
    V,VR,r,C = p.V, p.VR, p.r, p.C
    Vext(t) = 0.1*sin(t)
    qL,qR = q[1],q[2]
    dq[1] = V - sin(qL) - 1/C * (qL - qR) - Vext(t)
    dq[2] = - VR * sin(qR) + r/C * (qL - qR) + r*Vext(t)
end

### Solving the system

p = PS(0.5,0.5,0.5,2.0)

q0 = [0.,0.]
tspan = (0.0,2000)

prob = ODEProblem(phaseslip!,q0,tspan,p)
@time sol = solve(prob,dtmax=1.)

```

---

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [November 14, 2019, 8:08pm UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092/5 "2019-11-14T20:08:58Z")

</div>

The solution for me would be something within the lines of the following code:

```julia
function interactiveinclude(fname,h)
    hnew = hash(readlines(fname))
    if hnew!=h
        @eval include($fname)
    end
    return hnew
end

#include("formulation.jl")
function interactiveinclude(fname)
    @eval begin
        isdefined(Main,:h) || (h = nothing)
        h = interactiveinclude($fname,h)
    end
end

interactiveinclude("formulation.jl")

```

A dirty fix but works as expected. Does anyone already have a package for that?

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [November 15, 2019, 11:36am UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092/6 "2019-11-15T11:36:10Z")

</div>

I guess Revise.jl does something like that, but only for packages?

---

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [November 15, 2019, 12:07pm UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092/7 "2019-11-15T12:07:29Z")

</div>

Yes, and it does that job quite well. I always use it when I develop packages. However, I am not sure if I do want to structure my research code as a package. Part of the reason being that I am still trying to sell Julia to my supervisor, who just figured out that it is a great idea to enforce Python on the group because everyone uses it. Commonality I think could help a lot.

BTW I really think that over-definition of a method should never happen if an exact definition of method or a type is already loaded.

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [November 15, 2019, 12:28pm UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092/8 "2019-11-15T12:28:09Z")

</div>

Revise works great for individual scripts as well as for packages. See the `includet` function and the documentation here:

[https://timholy.github.io/Revise.jl/stable/cookbook/#includet-usage-1](https://timholy.github.io/Revise.jl/stable/cookbook/#includet-usage-1)

Note that if the problem is recompilation of methods that are actually being edited rather than package loading or recompilation of unchanged methods, first use after redefinition will still be slow.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 15, 2019, 12:33pm UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092/9 "2019-11-15T12:33:14Z")

</div>

> [@Janis\_Erdmanis](#):
>
> not sure if I do want to structure my research code as a package

Yes, you definitely want to do that. The main advantage is being able to commit the `Manifest.toml` to your version control repository, resulting an perfectly reproducible environment (as far as dependencies are concerned).

Once you start relying on it, this is one of those “how did I live without this?” features.

> [@Janis\_Erdmanis](#):
>
> enforce Python on the group because everyone uses it

You should still be able to interface with code from others using one of the Python interop packages:

> **[JuliaPy](https://github.com/JuliaPy)**
>
> Software that connects the Julia and Python languages. - JuliaPy

---

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [November 17, 2019, 10:04am UTC](https://discourse.julialang.org/t/why-the-code-recompiles/31092/10 "2019-11-17T10:04:35Z")

</div>

I changed structure of my code to the one of the package. Indeed I am thinking why I did not do that earlier 😉 It also fixed the issue of the recompilation as `using MyModule` always runs quick.
