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

**URL:** <https://discourse.julialang.org/t/is-using-btime-before-the-include-code-paralell-jl-correct-for-measuring-the-time-of-multi-threaded-code/95635>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 6, 2023, 9:11pm UTC](https://discourse.julialang.org/t/is-using-btime-before-the-include-code-paralell-jl-correct-for-measuring-the-time-of-multi-threaded-code/95635 "2023-03-06T21:11:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [March 6, 2023, 9:11pm UTC](https://discourse.julialang.org/t/is-using-btime-before-the-include-code-paralell-jl-correct-for-measuring-the-time-of-multi-threaded-code/95635/1 "2023-03-06T21:11:58Z")

</div>

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
$ 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:

```julia
############### 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
$ 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?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 6, 2023, 9:17pm UTC](https://discourse.julialang.org/t/is-using-btime-before-the-include-code-paralell-jl-correct-for-measuring-the-time-of-multi-threaded-code/95635/2 "2023-03-06T21:17:06Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [March 6, 2023, 10:18pm UTC](https://discourse.julialang.org/t/is-using-btime-before-the-include-code-paralell-jl-correct-for-measuring-the-time-of-multi-threaded-code/95635/3 "2023-03-06T22:18:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 13, 2023, 11:43am UTC](https://discourse.julialang.org/t/is-using-btime-before-the-include-code-paralell-jl-correct-for-measuring-the-time-of-multi-threaded-code/95635/4 "2023-03-13T11:43:58Z")

</div>

> [@Amro](#):
>
> 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?

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

> [@Amro](#):
>
> 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?

That depends on what the code being `include`d 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.
