Is using @btime before the include("code_paralell.jl") correct for measuring the time of multi-threaded code?

I have a file code_paralell.jl in which I am parallelizing its for-loop of simulation time. At the beginning, I am preparing some variables, in which some needs @eval in front of them (due to world age).
To measure the function computation time:
1- First I put @time before the function (i.e., @time f_parallel(args)). Then run julia and call the code. However, I was always seeing the compilation time even when I re-include the code consequently in the REPL.

$ julia --project=. -t 2
julia>  include("code_paralell.jl")
julia>  include("code_paralell.jl")

2- To pass that compilation time, I dropped the @time in front of the function and instead put @btime when calling the code in the REPL, as shown below:

############### code_paralell.jl #####################
using SparseArrays, LinearAlgebra, .Threads
# Initilize some variables, in which some are @eval
simulationTime = 1:10000
function f_parallel(args)
    # do serial work
    for t in simulationTime
            # do serial work
            @threads for i = 1:nthreads()
            # do parallel work
            end
            # do serial work
    end # for
    # do serial work
end # f_parallel(args)
f_parallel(args)
$ julia --project=. -t 2
julia> using BenchmarkTools
julia> @btime include("code_paralell.jl")

Is what I am doing correct, either in the first or the second case?

When you have include in the @btime, you’re also measuring compliation time of the function, since the included file redefines the function.

When you put @time in front of the call, you still get recompilation, because include then redefines your function. You’ll want to put @btime just in front of the actual call you want to measure (f_parallel(args) in this case).

1 Like

Thank you for your reply.
1- So, putting @btime just in front of the actual call you want to measure (f_parallel(args) in this case) will drop the compilation time for the multi-threaded code?
2- Is putting @time in front of include, or around a code that needs to be parallelized, is a fair comparison between the serial and parallel codes?

1 Like

Yes, it will do a precompilation run. Whether the code is multithreaded or not doesn’t matter.

That depends on what the code being included does internally - @time just measures the time it takes between the start of the macro and its end. There’s no consideration for what the code does.