# Run a loop for a set amount of time

**URL:** <https://discourse.julialang.org/t/run-a-loop-for-a-set-amount-of-time/82026>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 1, 2022, 3:13am UTC](https://discourse.julialang.org/t/run-a-loop-for-a-set-amount-of-time/82026 "2022-06-01T03:13:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adi\_Rai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adi_rai/32/34413_2.png) [@Adi\_Rai](https://discourse.julialang.org/u/Adi_Rai)\
**Post date:** [June 1, 2022, 3:13am UTC](https://discourse.julialang.org/t/run-a-loop-for-a-set-amount-of-time/82026/1 "2022-06-01T03:13:19Z")

</div>

Hi Everyone,

I was converting some MATLAB code to Julia. In MATLAB to run a while loop for a set period is done using tic and toc functions. I was struggling with implementing the same thing in Julia.

My question in short is how to run a while loop for a specific period of time in Julia.

Any help would be appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 1, 2022, 3:37am UTC](https://discourse.julialang.org/t/run-a-loop-for-a-set-amount-of-time/82026/2 "2022-06-01T03:37:04Z")

</div>

```julia
julia> function run(s)
           t = time()
           while time() < t + s
               sleep(0.001)
           end
       end
run (generic function with 1 method)

julia> @time run(0.1)
  0.101925 seconds (231 allocations: 6.766 KiB)

julia> @time run(0.5)
  0.502150 seconds (1.14 k allocations: 34.453 KiB)

julia> @time run(1.2)
  1.200282 seconds (2.75 k allocations: 83.469 KiB)

```

this is probably close to your tic toc in MATLAB

---

<div class="post-metadata">

**Author:** ![Adi\_Rai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adi_rai/32/34413_2.png) [@Adi\_Rai](https://discourse.julialang.org/u/Adi_Rai)\
**Post date:** [June 1, 2022, 6:25pm UTC](https://discourse.julialang.org/t/run-a-loop-for-a-set-amount-of-time/82026/3 "2022-06-01T18:25:04Z")

</div>

That is exactly what I was looking for! Thanks alot.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [June 1, 2022, 6:41pm UTC](https://discourse.julialang.org/t/run-a-loop-for-a-set-amount-of-time/82026/4 "2022-06-01T18:41:36Z")

</div>

wondering why allocations depend on s…

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 1, 2022, 6:42pm UTC](https://discourse.julialang.org/t/run-a-loop-for-a-set-amount-of-time/82026/5 "2022-06-01T18:42:47Z")

</div>

> [@Is it necessary for sleep to allocate?](https://discourse.julialang.org/t/is-it-necessary-for-sleep-to-allocate/77795):
>
> I was optimizing a complex block of multithreaded code and discovered that sleep was allocating. julia\> @btime sleep(0) 1.720 μs (5 allocations: 144 bytes) I was pretty surprised, so checked the [source](https://github.com/JuliaLang/julia/blob/2ba3a22858938ecaf41bbe3e780df507556825c5/base/asyncevent.jl#L232) which is basically wait(Timer(sec)), so it’s allocating a mutable struct for that Timer. Is there a more efficient alternative to that to sleep for a very short time? I found a [reference to usleep](https://discourse.julialang.org/t/accuracy-of-sleep/5546/3) but I’m on Windows so that’s not an option for me. I need a thread to sleep briefly (millisecon…

---

<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:** [June 1, 2022, 9:36pm UTC](https://discourse.julialang.org/t/run-a-loop-for-a-set-amount-of-time/82026/6 "2022-06-01T21:36:20Z")

</div>

You can use sleep\_ms(1) from the package [Timers](https://github.com/ufechner7/Timers.jl), much more accurate and non-allocating…

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 1, 2022, 10:41pm UTC](https://discourse.julialang.org/t/run-a-loop-for-a-set-amount-of-time/82026/7 "2022-06-01T22:41:40Z")

</div>

Probably in a real application you wouldn’t call `sleep` at all—your loop would be doing enough work that the overhead of occasional `time` calls would be negligible. (If not, you should just check the time every N-th iteration for a sufficiently large N.)
