governor

Loop speed limiter and timers

https://github.com/coolbutuseless/governor

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.6%) to scientific vocabulary
Last synced: 11 months ago · JSON representation

Repository

Loop speed limiter and timers

Basic Info
  • Host: GitHub
  • Owner: coolbutuseless
  • License: other
  • Language: C
  • Default Branch: main
  • Size: 43.9 KB
Statistics
  • Stars: 8
  • Watchers: 2
  • Forks: 0
  • Open Issues: 1
  • Releases: 0
Created about 2 years ago · Last pushed over 1 year ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

library(governor)
```

# governor


![](https://img.shields.io/badge/cool-useless-green.svg)
[![CRAN](http://www.r-pkg.org/badges/version/governor)](https://cran.r-project.org/package=governor)
[![R-CMD-check](https://github.com/coolbutuseless/governor/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/coolbutuseless/governor/actions/workflows/R-CMD-check.yaml)


`{governor}` is a [governor](https://en.wikipedia.org/wiki/Governor_(device)) (or *speed limiter*)
which limits the rate at which a *for*-loop or *while*-loop will run.

The total execution speed is limited by inserting short pauses based upon the 
time to run through the loop.  The waiting time is adjusted continuously 
to meet the target duration.


### What's in the box

* `gov_init()`, `gov_wait()` for limiting the execution speed of a for-loop, 
  while-loop or repeated function call.
* `gov_disable()`, `gov_enable()` for disabling/re-enabling a governor.  When 
  a governor is disabled, `gov_wait()` always returns immediately without waiting.
* `timer_init()`, `timer_check()` for setting alarms such that
  `timer_check()` will return `TRUE` after the given time has elapsed
* `timer_disable()`, `timer_enable()` for disabling/re-enabling a timer.  When 
  a timer is disabled it always immediately returns `FALSE`

## Installation

This package can be installed from CRAN

``` r
install.packages('governor')
```

You can install the latest development version from
[GitHub](https://github.com/coolbutuseless/governor) with:

``` r
# install.package('remotes')
remotes::install_github('coolbutuseless/governor')
```

Pre-built source/binary versions can also be installed from
[R-universe](https://r-universe.dev)

``` r
install.packages('governor', repos = c('https://coolbutuseless.r-universe.dev', 'https://cloud.r-project.org'))
```

## Running a loop at a rate of 30 times/second

A common use for `{governor}` will be timing animation at, say, 30 frames per second.

```{r example}
library(governor)

# Run loop at 30fps if possible
gov <- gov_init(1/30); 

# Running the loop 30 times at 30 frames-per-second should take ~1 second
# The actual work in this loop only takes 0.3seconds (30 * 0.01)
# So `gov_wait()` will pause every loop to maintain the interval
system.time({
  for (i in 1:30) {
    Sys.sleep(0.01)  # Work done in loop
    gov_wait(gov)    # Compensate to keep interval loop time
  }
})
```



## Skipping frames

When the actual work in the loop is fast, `{governor}` can compensate by waiting 
a longer amount of time.

When the work in the loop is slow, then `{governor}` can advise that the work 
for the next frame be skipped.  

In this example, we want the loop to run at 30 frames per second (i.e. an interval of 0.033 seconds), but 
the work itself takes 0.04 seconds.   The return value of `gov_wait()` is a
logical value indicating whether it is recommended that the next frame is skipped in 
order to achieve the desired loop interval.

In the output from this code, the `skip` variable is printed to show that `gov_wait()`
is advising that many frames should be skipped.

```{r skip}
library(governor)

# Run loop at 30fps if possible
# Set a high learning rate so it will converge quickly
gov <- gov_init(1/30); 

# Running the loop 30 times at 30 frames-per-second should take ~1 second
# The actual work should take a total of 0.1 * 30 = 3 seconds!
system.time({
  skip <- FALSE
  for (i in 1:30) {
    if (!skip) {
      Sys.sleep(0.1)  # Work done in loop
    }
    skip <- gov_wait(gov)    # Compensate to keep interval loop time
    cat(skip, "\n")
  }
})
```


## Setting timers

Timers are like alarm clocks that will return `TRUE` when the given duration 
has elapsed.

After returning `TRUE`, the timer will reset internally such that they will
trigger again after another period has elapsed.

```{r}
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
}
```



Owner

  • Name: mikefc
  • Login: coolbutuseless
  • Kind: user
  • Location: Australia

Cool, but useless.

GitHub Events

Total
  • Issues event: 1
  • Push event: 2
Last Year
  • Issues event: 1
  • Push event: 2

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 17
  • Total Committers: 1
  • Avg Commits per committer: 17.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 17
  • Committers: 1
  • Avg Commits per committer: 17.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
mike m****c@c****m 17
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 12 months ago

All Time
  • Total issues: 2
  • Total pull requests: 0
  • Average time to close issues: 8 days
  • Average time to close pull requests: N/A
  • Total issue authors: 1
  • Total pull request authors: 0
  • Average comments per issue: 0.5
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 2
  • Pull requests: 0
  • Average time to close issues: 8 days
  • Average time to close pull requests: N/A
  • Issue authors: 1
  • Pull request authors: 0
  • Average comments per issue: 0.5
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • coolbutuseless (2)
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 112 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 2
  • Total maintainers: 1
cran.r-project.org: governor

Speed Limiter to Control Rate of Execution of Loops

  • Versions: 2
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 112 Last month
Rankings
Dependent packages count: 28.5%
Dependent repos count: 35.2%
Average: 50.1%
Downloads: 86.7%
Maintainers (1)
Last synced: 11 months ago

Dependencies

.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/rhub.yaml actions
  • r-hub/actions/checkout v1 composite
  • r-hub/actions/platform-info v1 composite
  • r-hub/actions/run-check v1 composite
  • r-hub/actions/setup v1 composite
  • r-hub/actions/setup-deps v1 composite
  • r-hub/actions/setup-r v1 composite
DESCRIPTION cran
.devcontainer/Dockerfile docker
  • ghcr.io/rocker-org/devcontainer/tidyverse ${VARIANT} build
.devcontainer/requirements.txt pypi
  • ydiff * development