After the packages are loaded, there is a “long” time to wait until the first message indicate that the function called is running. Here is a test example:
println("------------- Loading")
t0 = time_ns()
using LinearAlgebra
using Random
t1 = time_ns()
println("Package loaded: ", t1 - t0, " ns")
println("------------- Going main")
t0 = time_ns()
function main() #test time_ns used
println("\n\n\n------------- running -------------")
t0 = time_ns()
N = 7777
Random.seed!(12357)
X = rand(N, N)
x = norm(X)
display(x)
t1 = time_ns()
println("code run time_ns: ", t1 - t0, " ns")
end
t1 = time_ns()
println("Reach main: ", t1 - t0, " ns ------------")
main()
nothing
You will see there is a time interval between “Reach main” and “------------- running -------------”, even in a consecutive run.
What jobs do Julia do during this time, just the overhead for calling a function, or else?
Julia has to compile the function upon the first run. When you call it again, it will be instant (within the same REPL session), but the first call will take a second or two to compile.
What do you mean by “consecutive run”? I’m guessing that you have stored all the code you posted into a file, and that you generate consecutive runs by including that file multiple times. If so, then each time the file is included, the function main is redefined and must be recompiled when called. That’s why you see the delay each time.
Now the next step is to figure out how to cache the compilation. There are a few options:
Do not exit Julia. The compile code will be stored in memory (RAM). You can run julia with -i and enter the interactive REPL. Using Revise.jl makes this easier.
You are doing interactive exploration. Solution: use an interactive environment (the REPL, a Jupyter notebook, vsCode, Pluto, …) in which you leave Julia running. (For larger-scale code development, do this in conjunction with Revise.jl by creating a package. See Best practise: organising code in Julia - #2 by stevengj)
Your function main() is extremely fast, but you are calling it lots of times. Solution: write your loop in Julia — don’t write a shell script or something. (If you are doing benchmarking, use BenchmarkTools.jl.)
Your function main() takes a long time (minutes, hours, …). Solution: use separate Julia runs if you want. The compile time will be irrelevant.
I do wonder now that we have pkgimgs how long would it take to get scriptimgs . We seem to be getting quite close to just compiling this to a not very portable executable.
I am excited that Julia 1.9 rc1 cut the first run of the EfficientFrontier.ECL for “Status-Segment Method” from 7.89 seconds (in Julia 1.8.5) seconds to 0.13 seconds. However, how to reduce the 9.42 seconds in the first run of calling function main? (the data have been downloaded to /tmp/sp500.jls.xz first )