Do you need to run function (from compiled module) twice to get performance benefit in Julia script?


I am writing a module with couple functions to be used in a script. If I run the script that using functions from the precompiled module, do I still need to include a small dataset to compile the functions? Here might be an inappropriate example.

the scripts

using CSV, DataFrames
@time a = CSV.File(ARGS[1];delim='\t',header=false) |> DataFrame
@time b = CSV.File(ARGS[1];delim='\t',header=false) |> DataFrame

run the script on shell
julia test.jl

7.014790 seconds (19.07 M allocations: 917.315 MiB, 7.23% gc time)
0.005464 seconds (6.90 k allocations: 972.328 KiB)

Functions are compiled on demand, so they will be compiled (for given argument types) the first time you use them.

Consequently, running the function just to get it compiled is not necessary. If you start with b =, it will be compiled then.

It is a dumb question. The time of function’s first run is the sum of compile time plus function-run time (t1). The second time the function runs, it only have function running time t2. Is it t1 nearly the same as t2?

Thank you

Completely depends on the problem. But the real answer is how they scale. t1 is independent of the values in your problem and is just dependent on the code, and is generally around a second or so. t2 is completely dependent on the values.

For example, if you have a program that takes about 5ms to run, it might take about 1s to compile and 5ms to run, so two runs is 1s and 10ms. If you have a program that takes 2 hours to run, the cost to run it twice is 4 hours and 1 second. Notice that in the second case, if you just want to run the program once (which is reasonable :smile:), then it’s 2 hours and 1 second if you just run it once. Compile time is additive: there’s no reason to run twice to try and magically make code faster.

Moral of the story, for any decently difficult calculation the compile time will not be a real factor, though for quick checks when playing interactively in the REPL it’s a noticeable bump.