File: pause.R

package info (click to toggle)
r-cran-profvis 0.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 868 kB
  • sloc: javascript: 1,943; ansic: 41; sh: 13; makefile: 8
file content (30 lines) | stat: -rw-r--r-- 1,013 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#' Pause an R process
#'
#' This function pauses an R process for some amount of time. It differs from
#' [Sys.sleep()] in that time spent in `pause` will show up in
#' profiler data. Another difference is that `pause` uses up 100\% of a CPU,
#' whereas `Sys.sleep` does not.
#'
#' @examples
#' # Wait for 0.5 seconds
#' pause(0.5)
#'
#' @param seconds Number of seconds to pause.
#' @useDynLib profvis, .registration = TRUE, .fixes = "c_"
#' @export
pause <- function(seconds) {
  if (is.integer(seconds)) {
    seconds <- as.numeric(seconds)
  }
  .Call(c_profvis_pause, seconds)
}

# This guarantees that (1) `pause()` is always compiled, even on
# `load_all()` and (2) it doesn't include source references. This in
# turn ensures consistent profile output: if the function is not
# compiled and doesn't contain srcrefs, `.Call()` is never included in
# the profiles, even when `line.profiling` is set.
on_load_pause <- function() {
  pause <<- utils::removeSource(pause)
  pause <<- compiler::cmpfun(pause)
}