I have a module MyMod
in MyMod.jl
, which contains all functions I am working on. The functions in MyMod
are kind of stable now and not changed frequently, but I need to use them for some other tests.
What I currently do is
# some julia file
include("../src/MyMod.jl")
import MyMod
# Then doing something with funcions from MyMod
The problem is that I need to rerun this file many times, and the loading time is dominated by including the MyMod
, which takes 1-2 seconds. This is certainly not long, but kind of irritating.
I curious if there is a simple way to speed this procedure. I know making the code into a package should do the job, but it seems a little complicated and I’m not sure if it’s worth the time (I never did that before, and still use Julia 1.8, BTW.)
I found I can use push!(LOAD_PATH, "../src/")
and then import MyMod
. This way the file is also precompiled. Does this speed up the loading time? Is there any disadvantage of add a custom path to LOAD_PATH
?
Thanks.
I found I can use push!(LOAD_PATH, "../src/")
and then import MyMod
.
The LOAD_PATH approach is correct. You can also put packages in your .julia/dev
folder and then it’s already on the path.
This way the file is also precompiled
(if it’s a module)
I know making the code into a package should do the job, but it seems a little complicated and I’m not sure if it’s worth the time (I never did that before, and still use Julia 1.8, BTW.)
If you care about load times and getting modules precompiled, you should be using Julia 1.9. Everything is better in 1.9.
Also, once you do it a couple of times, creating a package isn’t that hard, and it’s scarcely different from writing your file as a module in your src/
directory. Use Pkg.generate
for packages that only you will use, PkgTemplates for anything intended for broader consumption. Having the skeletons for testing etc helps encourage you to test and design for reusability, which isn’t a bad thing. But it’s also fine to just create “package-like” modules (really there is very little gap between these).
Thank you!
I think my problem is from the structure of the current repo. The repo contains the MyMod
module which contains main functions. However, I have have many “test” files, which are more like scripts, using the main functions. These are not simple tests. They also contains quite some code, and I’m thinking if I should precompile them in some way.
I think my real problem is how to structure the project. Roughly speaking, I have a “lib” (the MyMod
) and some main programs. They belong to the same research project, so they are in the same repo. I tend to not separate them, but maybe that’s not the best way to go…?