# Package import order change load time

**URL:** <https://discourse.julialang.org/t/package-import-order-change-load-time/73220>\
**Category:** Performance\
**Tags:** question\
**Created:** [December 16, 2021, 5:51pm UTC](https://discourse.julialang.org/t/package-import-order-change-load-time/73220 "2021-12-16T17:51:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [December 16, 2021, 5:51pm UTC](https://discourse.julialang.org/t/package-import-order-change-load-time/73220/1 "2021-12-16T17:51:48Z")

</div>

It seems like the order of loading packages can have some impact on the time, mostly got curious about why this happened.

Looking at two packages for different load configurations I get

```julia
albheim@gilbert ~> hyperfine "julia --project=@control -e 'using Plots; using ControlSystems'"
Benchmark 1: julia --project=@control -e 'using Plots; using ControlSystems'
  Time (mean ± σ): 16.764 s ± 0.683 s [User: 16.381 s, System: 1.196 s]
  Range (min … max): 16.027 s … 18.150 s 10 runs
 
albheim@gilbert ~> hyperfine "julia --project=@control -e 'using ControlSystems; using Plots'"
Benchmark 1: julia --project=@control -e 'using ControlSystems; using Plots'
  Time (mean ± σ): 19.338 s ± 1.034 s [User: 18.889 s, System: 1.288 s]
  Range (min … max): 18.319 s … 20.993 s 10 runs

albheim@gilbert ~> hyperfine "julia --project=@control -e 'using Plots, ControlSystems'"
Benchmark 1: julia --project=@control -e 'using Plots, ControlSystems'
  Time (mean ± σ): 16.328 s ± 0.807 s [User: 15.978 s, System: 1.234 s]
  Range (min … max): 15.557 s … 17.741 s 10 runs

albheim@gilbert ~> hyperfine "julia --project=@control -e 'using ControlSystems, Plots'"
Benchmark 1: julia --project=@control -e 'using ControlSystems, Plots'
  Time (mean ± σ): 20.156 s ± 1.245 s [User: 19.665 s, System: 1.279 s]
  Range (min … max): 18.879 s … 22.500 s 10 runs

```

which seems to show that `using A, B` is the same as `using A; using B` but different from `using B, A`.

I know that `ControlSystems` depends on `Plots`, so maybe there is something from there? Can we say that `using A, B` will be better than `using B, A` when `A` is a dependency of `B` in general? Or was that just a coincidence here?

It is a large enough difference that I feel it might be important for packages to think about the order they import things, but maybe this is just a special case?

Will look at it more when I have time, just thought I would post to see if anyone knew more.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [December 18, 2021, 10:23am UTC](https://discourse.julialang.org/t/package-import-order-change-load-time/73220/2 "2021-12-18T10:23:12Z")

</div>

When you have this effect I believe that’s an indication that there may be some methods invalidations going on, which force recompilation.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [December 18, 2021, 4:07pm UTC](https://discourse.julialang.org/t/package-import-order-change-load-time/73220/3 "2021-12-18T16:07:59Z")

</div>

Yeah, I was thinking that might be the case.

I tried the same with a few others package combinations and couldn’t really see the same effect so maybe not something worth spending too much time on. Was mostly thinking if there was an easy heuristic to follow, like always load the package with least deps first, and then comes the packages that depends on that one. But it seems like this might might have been a case that was a bit more extreme than normal.
