Title: | Speed Limiter to Control Rate of Execution of Loops |
---|---|
Description: | It can be necessary to limit the rate of execution of a loop or repeated function call e.g. to show or gather data only at particular intervals. This package includes two methods for limiting this execution rate; speed governors and timers. A speed governor will insert pauses during execution to meet a user-specified loop time. Timers are alarm clocks which will indicate whether a certain time has passed. These mechanisms are implemented in 'C' to minimize processing overhead. |
Authors: | Mike Cheng [aut, cre, cph] |
Maintainer: | Mike Cheng <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3 |
Built: | 2024-11-07 12:23:16 UTC |
Source: | https://github.com/coolbutuseless/governor |
When disabled a governor always returns immediately without any waiting
gov_enable(gov) gov_disable(gov)
gov_enable(gov) gov_disable(gov)
gov |
object created by |
None.
gov <- gov_init(1/30) gov_disable(gov) gov_enable(gov)
gov <- gov_init(1/30) gov_disable(gov) gov_enable(gov)
Initialize a governor to control the speed of a loop
gov_init(interval, alpha = 0.4, alpha_decay = 0.95, alpha_target = 0.05)
gov_init(interval, alpha = 0.4, alpha_decay = 0.95, alpha_target = 0.05)
interval |
desired interval in seconds E.g. |
alpha |
initial learning rate used to adjust wait time. Default: 0.4 |
alpha_decay |
rate at which alpha decays. Default: 0.95 i.e. 5 each iteration |
alpha_target |
the baseline alpha to reach when running long term. default: 0.05 |
gov object to be used with gov_wait()
# This loop should take approx 1 second as the governor will limit # the loop to run every thirtieth of a second. gov <- gov_init(1/30) system.time({ for (i in 1:30) { gov_wait(gov) } })
# This loop should take approx 1 second as the governor will limit # the loop to run every thirtieth of a second. gov <- gov_init(1/30) system.time({ for (i in 1:30) { gov_wait(gov) } })
gov
Wait an appropriate amount of time within a for-loop to match the desired
interval set in gov
gov_wait(gov)
gov_wait(gov)
gov |
object created by |
Logical value. If TRUE
then the governor is suggesting that
the work for next loop be skipped if the loop interval is to
be maintained in the long term.
# This loop should take approx 1 second gov <- gov_init(1/30) # interval = 0.0333 seconds system.time({ for (i in 1:30) { Sys.sleep(0.01) # Work done in loop gov_wait(gov) # Compensate to keep interval loop time } })
# This loop should take approx 1 second gov <- gov_init(1/30) # interval = 0.0333 seconds system.time({ for (i in 1:30) { Sys.sleep(0.01) # Work done in loop gov_wait(gov) # Compensate to keep interval loop time } })
Check the status of a timer
timer_check(timer)
timer_check(timer)
timer |
timer object returned by |
logical indicating if timer was triggered. If TRUE
, then
the internal state of the timer will be reset (so that it will
trigger again after another duraction
has elapsed)
# Run two timers in a tight while loop # The short timer should trigger every 0.1 seconds # The long timer will trigger after 1 second # Note that the timers will reset every time they trigger (after returning TRUE) long_timer <- timer_init(1) short_timer <- timer_init(0.1) counter <- 0L while(TRUE) { if (timer_check(long_timer)) { cat("\nLong timer fired at count: ", counter, "\n") break; } if (timer_check(short_timer)) { cat("Short timer fired at count: ", counter, "\n") } counter <- counter + 1L }
# Run two timers in a tight while loop # The short timer should trigger every 0.1 seconds # The long timer will trigger after 1 second # Note that the timers will reset every time they trigger (after returning TRUE) long_timer <- timer_init(1) short_timer <- timer_init(0.1) counter <- 0L while(TRUE) { if (timer_check(long_timer)) { cat("\nLong timer fired at count: ", counter, "\n") break; } if (timer_check(short_timer)) { cat("Short timer fired at count: ", counter, "\n") } counter <- counter + 1L }
When disabled a timer always immediately returns FALSE
timer_enable(timer) timer_disable(timer)
timer_enable(timer) timer_disable(timer)
timer |
timer object returned by |
None.
timer <- timer_init(1) timer_disable(timer) timer_enable(timer)
timer <- timer_init(1) timer_disable(timer) timer_enable(timer)
TRUE
when checked and the
given duration has elapsed.The timer will automatically reset any time it is checked (via check_timer()
)
and it returns TRUE
.
timer_init(duration, reset_mode = "checked")
timer_init(duration, reset_mode = "checked")
duration |
timer duration in seconds |
reset_mode |
one of 'checked' (default) or 'created' . If 'checked', then
when the timer is reset the next alarm will be set to |
a timer
object to used with timer_check()
# Run two timers in a tight 'while' loop # The short timer should trigger every 0.1 seconds # The long timer will trigger after 1 second # Note that the timers will reset every time they trigger (after returning TRUE) long_timer <- timer_init(1) short_timer <- timer_init(0.1) counter <- 0L while(TRUE) { if (timer_check(long_timer)) { cat("\nLong timer fired at count: ", counter, "\n") break; } if (timer_check(short_timer)) { cat("Short timer fired at count: ", counter, "\n") } counter <- counter + 1L }
# Run two timers in a tight 'while' loop # The short timer should trigger every 0.1 seconds # The long timer will trigger after 1 second # Note that the timers will reset every time they trigger (after returning TRUE) long_timer <- timer_init(1) short_timer <- timer_init(0.1) counter <- 0L while(TRUE) { if (timer_check(long_timer)) { cat("\nLong timer fired at count: ", counter, "\n") break; } if (timer_check(short_timer)) { cat("Short timer fired at count: ", counter, "\n") } counter <- counter + 1L }