# Several questions about time evaluation with @time

**URL:** <https://discourse.julialang.org/t/several-questions-about-time-evaluation-with-time/119455>\
**Category:** Performance\
**Tags:** question, performance\
**Created:** [September 16, 2024, 7:32am UTC](https://discourse.julialang.org/t/several-questions-about-time-evaluation-with-time/119455 "2024-09-16T07:32:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 16, 2024, 7:32am UTC](https://discourse.julialang.org/t/several-questions-about-time-evaluation-with-time/119455/1 "2024-09-16T07:32:40Z")

</div>

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

```julia
total = 0
for i in 1:100000000
    total += i
end

```

1. Standard practice is as follows (right?): First, write it into a function:

```julia
function mysum()
    total = 0
    for i in 1:100000000
        total += i
    end
end

```

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

```julia
@time mysum()

```

Finally, run again, formally timing it:

```julia
@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?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 16, 2024, 7:53am UTC](https://discourse.julialang.org/t/several-questions-about-time-evaluation-with-time/119455/2 "2024-09-16T07:53:42Z")

</div>

> [@WuSiren](#):
>
> Then how do we record the return value of @time into a variable and store it?

Use the macro `@timed`

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

```

The help function `?@timed` gives more details.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [September 16, 2024, 8:02am UTC](https://discourse.julialang.org/t/several-questions-about-time-evaluation-with-time/119455/3 "2024-09-16T08:02:35Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 16, 2024, 8:29am UTC](https://discourse.julialang.org/t/several-questions-about-time-evaluation-with-time/119455/4 "2024-09-16T08:29:44Z")

</div>

> [@abraemer](#):
>
> Yes I think you should ensure that every worker has run the function because each Julia process needs to do the compilation by itself.

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?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [September 16, 2024, 8:33am UTC](https://discourse.julialang.org/t/several-questions-about-time-evaluation-with-time/119455/5 "2024-09-16T08:33:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 16, 2024, 8:42am UTC](https://discourse.julialang.org/t/several-questions-about-time-evaluation-with-time/119455/6 "2024-09-16T08:42:38Z")

</div>

Oh, I see. Many thanks! 🤝🤝

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [September 16, 2024, 2:20pm UTC](https://discourse.julialang.org/t/several-questions-about-time-evaluation-with-time/119455/7 "2024-09-16T14:20:01Z")

</div>

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

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

julia> stats.time
0.0014197

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

julia> t
0.0014014

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 16, 2024, 3:32pm UTC](https://discourse.julialang.org/t/several-questions-about-time-evaluation-with-time/119455/8 "2024-09-16T15:32:47Z")

</div>

Thanks!
