About reproducibility of Julia program run times

The behavior you’re experiencing is Julia’s Just-Ahead-Of-Time compilation.

When you execute code that defines a new function, no compilation happens. The first time that function is called for a specific set of input types the Julia compiler kicks in and compiles the optimal machine code for that set of input types.

As of Julia v1.9, packages can define precompilation routines to trigger the compiler at package installation time which are then cached and can be used between sessions. However, user code is not cached between sessions, so each time you start a new Julia session, the compiler has to recompile all the methods for functions you define and methods for functions that are defined by packages but not hit during the precompilation routine.

This “slow” first execution issue is called “Time to first execution” (TTFX) or sometimes “time to first plot.” If you search the Discourse for these phrases, you will find LOTS of discussion.

There are two remedies.

  1. PackageCompiler.jl as @karei mentioned. You will need to have some “dry run” code that will call all the methods you want precompiled. Warning – this produces a full Julia System image that is quite large.
  2. This is not yet published, but in Julia v1.12, there will be a new ahead-of-time Julia compiler that will produce smaller executables.
5 Likes