Several questions about time evaluation with @time

For example, if we want to evaluate the time cost of the following code snippet:

total = 0
for i in 1:100000000
    total += i
end
  1. Standard practice is as follows (right?): First, write it into a function:
function mysum()
    total = 0
    for i in 1:100000000
        total += i
    end
end

Then, run the function once and time it to trigger compilation:

@time mysum()

Finally, run again, formally timing it:

@time mysum()
  1. Now in the REPL we can see the time cost: 0.000001 seconds. However, in many cases, we need to store this time for subsequent analysis. Then how do we record the return value of @time into a variable and store it?
  2. If the function contains a pmap parallel mechanism internally, and we increase the number of workers by 5, i.e.using Distributed; addprocs(4), then in order to evaluate the running time of this function, should we ensure that the function has been pre-run on each worker?

Use the macro @timed

julia> stats = @timed rand(10^6);
  
  julia> stats.time
  0.006634834

The help function ?@timed gives more details.

2 Likes

Yes I think you should ensure that every worker has run the function because each Julia process needs to do the compilation by itself.

1 Like

Thank you.

If I add 4 workers, and now there are 5 workers in total, and then I run a program that contains two functions, the first function contains pmap parallel, and the second function is a thorough serial program. Then, I know that the first function is completed by multiple workers, and my question now is whether we can explicitly know which worker runs the second function?

If the serial function is randomly assigned to an idle worker to run, how do we ensure that this serial function is pre-run and compiled on each worker?

If you don’t use some parallel construct like pmap or @spawn then everything happens on the main process (id 1).

1 Like

Oh, I see. Many thanks! :handshake::handshake:

If you only need the .time portion of @timed, you can also just use @elapsed:

julia> stats = @timed rand(10^6);

julia> stats.time
0.0014197

julia> t = @elapsed rand(10^6);

julia> t
0.0014014
1 Like

Thanks!