# Time needed to perform a complete loop

**URL:** https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494
**Category:** General Usage
**Tags:** loops, time
**Created:** [July 3, 2020, 9:04pm UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494 "2020-07-03T21:04:16Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 3, 2020, 9:04pm UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/1 "2020-07-03T21:04:16Z")

</div>

I want to measure the total amount of time nedded to perform a complete loop. I tried

```julia
@time for i in 1:10000
...
...
end

```

and it returned `.988083 seconds (57.58 M allocations: 39.867 GiB, 0.14% gc time)` when the code ran about 15 minutes to complete the loop and not 0.988083 seconds as indicated. How can I measure time correctly?

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [July 3, 2020, 10:16pm UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/2 "2020-07-03T22:16:20Z")

</div>

Tell us what’s in the `...`? Normally what you did should work fine.

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 3, 2020, 10:30pm UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/3 "2020-07-03T22:30:31Z")

</div>

The loop calls a lot of functions I created outside of the loop and a dataset I have ☹ ☹ Since the code with the functions is large, I thought that putting just the command I’m using to measure time would be enough to know if I’m using the right command or placing wrongly `@time` ☹

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [July 3, 2020, 10:32pm UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/4 "2020-07-03T22:32:08Z")

</div>

It should all be fine unless you have `@async`s or threads or whatever. It’s easy to convince yourself that this should work fine:

```julia
julia> @time for i = 1:3
           sleep(1)
       end
  3.006142 seconds (15 allocations: 432 bytes)

```

So there has to be something else going on.

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 3, 2020, 10:34pm UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/5 "2020-07-03T22:34:47Z")

</div>

Ah thanks @tim.holy! Will check my code then

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [July 4, 2020, 12:29am UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/6 "2020-07-04T00:29:06Z")

</div>

`@time` often seems to miss compilation time, at least in generated functions:

```julia
julia> @generated function slow_to_compile(x)
           sleep(10)
           :(x)
       end
slow_to_compile (generic function with 1 method)

julia> t0 = time(); @time for i ∈ 1:3
           sleep(slow_to_compile(i))
       end; time() - t0
  6.009163 seconds (15 allocations: 432 bytes)
16.032392978668213

julia> versioninfo()
Julia Version 1.6.0-DEV.355
Commit 955beb6a91* (2020-07-02 10:28 UTC)

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [July 4, 2020, 1:03am UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/7 "2020-07-04T01:03:13Z")

</div>

This is a big of a tangent, but you shouldn’t be doing heavy computations in a globally scoped `for` loop as this will be quite bad for performance and memory usage. Consider using a `let` block or a function (and avoid global variables).

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 4, 2020, 1:08am UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/8 "2020-07-04T01:08:24Z")

</div>

Ok 🙂 Thanks for your help 🙂 I.m trying to see if I can measure the exact time with another command 🙂

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [July 4, 2020, 3:52am UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/9 "2020-07-04T03:52:20Z")

</div>

Just out of curiosity, are you using Juno? I saw it sometimes in Juno, that output of @time is being partially “eaten” in console, I guess it’s some weird bug.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [July 4, 2020, 5:54am UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/10 "2020-07-04T05:54:36Z")

</div>

[https://github.com/JuliaLang/julia/blob/66f41c58683d68b43ad0a2a9d8d605de72ca0de9/base/timing.jl#L171](https://github.com/JuliaLang/julia/blob/66f41c58683d68b43ad0a2a9d8d605de72ca0de9/base/timing.jl#L171)

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [July 4, 2020, 11:56pm UTC](https://discourse.julialang.org/t/time-needed-to-perform-a-complete-loop/42494/11 "2020-07-04T23:56:06Z")

</div>

Yes I’m using Juno, indeed!
