# \[ANN\] Timers.jl tic() toc() and accurate delays

**URL:** <https://discourse.julialang.org/t/ann-timers-jl-tic-toc-and-accurate-delays/81461>\
**Category:** Package Announcements\
**Created:** [May 22, 2022, 11:00am UTC](https://discourse.julialang.org/t/ann-timers-jl-tic-toc-and-accurate-delays/81461 "2022-05-22T11:00:59Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [May 22, 2022, 11:00am UTC](https://discourse.julialang.org/t/ann-timers-jl-tic-toc-and-accurate-delays/81461/1 "2022-05-22T11:00:59Z")

</div>

The package [GitHub - ufechner7/Timers.jl: Timers for Julia](https://github.com/ufechner7/Timers.jl) provides the helper functions `tic()` and `toc() that where removed from Julia in version 1.0.

I still find them very useful, for example to measure the time until a program is loaded.

In addition it provides the function:

```julia
delay_ms(time_ms)

```

with an accuracy of better than 100 nanoseconds ±0.2%, but only if the system is not fully loaded.

This is is much better than the accuracy of the function sleep() which has - for me - an error of  
16ms (!) on Windows and of 1 to 2 ms on Linux.

Finally it provides the function

```julia
wait_until(finish_ns; always_sleep=false)

```

that sleeps until the given time [ns] is reached. Example:

```julia
dt = 0.05
start = time_ns()
for i in 1:100
    # do some work
    wait_until(start + i*dt*1e9)
end

```

The section “`# do some work`” is executed exactly every 50ms as long as executing this section takes less than 50ms.

The additional parameter always\_sleep can be set to true if you want to ensure that the sleep function  
is always called for at least 1 ms. This is useful if you need to ensure that cooperative multitasking  
works even for the price to miss the deadline.

To test the accuracy on your machine, you can run:

```julia
using Pkg
pkg"test Timers"

```

If you want more Timers, please create an issue for the package on Github.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 22, 2022, 11:10am UTC](https://discourse.julialang.org/t/ann-timers-jl-tic-toc-and-accurate-delays/81461/2 "2022-05-22T11:10:39Z")

</div>

The accuracy seems to be much better.  
`sleep` seems to be 1 ms off by default.

```julia
7-element BenchmarkTools.BenchmarkGroup:
  tags: ["sleep_ms"]
  "100µs" => TrialEstimate(100.068 μs)
  "200µs" => TrialEstimate(200.068 μs)
  "5ms" => TrialEstimate(5.000 ms)
  "1ms" => TrialEstimate(1.000 ms)
  "10ms" => TrialEstimate(10.000 ms)
  "500µs" => TrialEstimate(500.067 μs)
  "2ms" => TrialEstimate(2.000 ms)
5-element BenchmarkTools.BenchmarkGroup:
  tags: ["sleep"]
  "20ms" => TrialEstimate(21.239 ms)
  "5ms" => TrialEstimate(6.299 ms)
  "1ms" => TrialEstimate(2.054 ms)
  "10ms" => TrialEstimate(11.306 ms)
  "2ms" => TrialEstimate(3.252 ms)

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [May 22, 2022, 2:48pm UTC](https://discourse.julialang.org/t/ann-timers-jl-tic-toc-and-accurate-delays/81461/3 "2022-05-22T14:48:33Z")

</div>

Good job!

[TickTock.jl](https://github.com/cormullion/TickTock.jl) says “Hi!” 🙂

---

<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:** [May 22, 2022, 3:37pm UTC](https://discourse.julialang.org/t/ann-timers-jl-tic-toc-and-accurate-delays/81461/4 "2022-05-22T15:37:53Z")

</div>

Thank you! Yes, a little bit of overlap with your package, but I want to implement more the functionality of the Timer class of QT to be used more by programs and not so much on the command line. I will also add Observables to bind functions to timer events.
