Measuring the time properly for nested for-loops with @parallel

Hi, I am trying to measure the total time used to execute a nested for-loops. I used @elapsed but it seems to only return the time used by one of the inner loops. A MWE is as follows:

function f(m,n)
    s = SharedArray{Float64}(m)
    for i = 1:m
        @distributed for j = 1:n 
            s[i] = s[i] + rand(1)[1]
        end
    end 
    return s
end

@elapsed f(500, 1e7)

The above MWE returns 0.000486948, which is not what I need because f(500, 1e7) takes over a minute to run. I tried the TickTock package too and it also has a similar issue.

Therefore, I am wondering what’s the appropriate way to count the time required. In the above MWE, I want to count the time required to run f(500, 1e7), not the time used by one of the loops inside the function f.

Thanks for the help in advance!

The macros from BenchmarkTools seem to report the correct times:

julia> using BenchmarkTools

julia> @belapsed f(100,1e6)
4.345676228

julia> @benchmark f(100,1e6)
BechmarkTools.Trial: 2 samples with 1 evaluations.
 Range (min … max):  4.834 s …   4.922 s  β”Š GC (min … max): 5.16% … 5.45%
 Time  (median):     4.878 s              β”Š GC (median):    5.31%
 Time  (mean Β± Οƒ):   4.878 s Β± 62.011 ms  β”Š GC (mean Β± Οƒ):  5.31% Β± 0.21%

  β–ˆ                                                       β–ˆβ–ˆ 
  β–ˆβ–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–β–ˆ ▁
  4.83 s         Histogram: frequency by time        4.92 s <

 Memory estimate: 8.94 GiB, allocs estimate: 100004891.


1 Like

This works. Thanks!