Julia is a great language, but the Just-in-Time compiler implies that loading a package could takes a considerable time, this is called the first plot problem.
It is true that this time is only required for the first time (and there are options, like using and the package Revise). However, it is a great disadvantage when we want to use Julia to create small scripts.
This package solve that problem. It is available at
Inspired in the daemon-mode of Emacs, this package create a server/client model. This allow julia to run scripts a lot quickly scripts in Julia, because the package is maintained in memory between the run of several scripts (or run the same script several times).
Usage
-
The server is the responsible of running all julia scripts.
julia -e 'using DaemonMode; serve()'
-
A client, that send to the server the file to run, and return the output obtained.
julia -e 'using DaemonMode; runargs()' program.jl <arguments>
you can use an alias
alias juliaclient='julia -e "using DaemonMode; runargs()"'
then, instead of
julia program.jl
you can dojuliaclient program.jl
. The output should be the same, but with a lot less time.
Process
The process is the following:
-
The client process sends the program program.jl with the required arguments to the server.
-
The server receives the program name, and run it, returning the output to the client process.
-
The client process receives the output and show it to the console.
Example
Supose that we have the script test.jl
using CSV, DataFrames
fname = only(ARGS)
df = CSV.File(fname) |> DataFrame
println(first(df, 3))
The normal method is:
$ time julia test.jl tsp_50.csv
...
3Γ2 DataFrame
β Row β x β y β
β β Float64 β Float64 β
βββββββΌβββββββββββΌβββββββββββββ€
β 1 β 0.420169 β 0.628786 β
β 2 β 0.892219 β 0.673288 β
β 3 β 0.530688 β 0.00151249 β
real 0m18.831s
user 0m18.670s
sys 0m0.476s
Only loading the CSV, DataFrames, and reading a simple file takes 18 seconds in my computer (I accept donnations :-)). Every time that you run the program is going to take these 18 seconds.
using DaemonMode:
$ julia -e 'using DaemonMode; serve()' &
$ time juliaclient test.jl tsp_50.csv
3Γ2 DataFrames.DataFrame
β Row β x β y β
β β Float64 β Float64 β
βββββββΌβββββββββββΌβββββββββββββ€
β 1 β 0.420169 β 0.628786 β
β 2 β 0.892219 β 0.673288 β
β 3 β 0.530688 β 0.00151249 β
real 0m18.596s
user 0m0.329s
sys 0m0.318s
But next times, it is a lot faster:
$ time juliaclient test.jl tsp_50.csv
3Γ2 DataFrames.DataFrame
β Row β x β y β
β β Float64 β Float64 β
βββββββΌβββββββββββΌβββββββββββββ€
β 1 β 0.420169 β 0.628786 β
β 2 β 0.892219 β 0.673288 β
β 3 β 0.530688 β 0.00151249 β
real 0m0.355s
user 0m0.336s
sys 0m0.317s
A reduction from 18s to 0.3s, the next runs only take a 2% of the original time.
Also, you can change the file (or run another one) and the performance is maintained:
test2.jl:
using CSV, DataFrames
fname = only(ARGS)
df = CSV.File(fname) |> DataFrame
println(last(df, 10))
$ time juliaclient test2.jl tsp_50.csv
10Γ2 DataFrames.DataFrame
β Row β x β y β
β β Float64 β Float64 β
βββββββΌβββββββββββΌβββββββββββ€
β 1 β 0.25666 β 0.405932 β
β 2 β 0.266308 β 0.426364 β
β 3 β 0.865423 β 0.232437 β
β 4 β 0.462485 β 0.049489 β
β 5 β 0.994926 β 0.887222 β
β 6 β 0.867568 β 0.302558 β
β 7 β 0.475654 β 0.607708 β
β 8 β 0.18198 β 0.592476 β
β 9 β 0.327458 β 0.354397 β
β 10 β 0.765927 β 0.806685 β
real 0m0.372s
user 0m0.369s
sys 0m0.300s