The package GitHub - ufechner7/Timers.jl: Timers for Julia 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:
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
wait_until(finish_ns; always_sleep=false)
that sleeps until the given time [ns] is reached. Example:
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:
using Pkg
pkg"test Timers"
If you want more Timers, please create an issue for the package on Github.