Perfomance issue when executing a script

I write a simple script, plotex.jl to produce a plot:

using Plots
pyplot() # Switch to using the PyPlot.jl backend
plot1 = plot(rand(10,10),linewidth=2,title=“Plot 1”)
display(plot1)

When I type the code manually into REPL the plot shows immediately after the last line. The plot also shows immediately if I execute the code from REPL with

julia > include ("plotex.jl")

When I run the code as a script from a terminal by

$ julia plotex.jl

it takes 27 seconds for the plot to appear! Can anyone explain this?

I am guessing that is you start a fresh REPL and include the file/run the code, it will also take about 27 seconds for the plot to appear.
The code must compile before it runs, and Plots.jl is notoriously slow to compile. Once it has compiled, it is fast.

If you already ran it once in the REPL, it won’t need to compile again within that session.

4 Likes

Time-to-first-plot is a well-known issue, see: Roadmap for a faster time-to-first-plot?

1 Like

Executing scripts in Julia is a little different than Python (for example). I would use the workflow of loading all the packages you need in the REPL first, and then you can repeatedly include your source file to run the script over and over. Or even better, you can put everything in your script in a main() function, includet the script using Revise.jl, and then run your main() function over and over again while making and saving changes to the script with a text editor. That’s the workflow I use and scripts always run really fast, after the initial package loading and compilation.

1 Like