Title: | 'hms' Based Timer |
---|---|
Description: | Tracks elapsed clock time using a `hms::hms()` scalar. It was was originally developed to time Bayesian model runs. It should not be used to estimate how long extremely fast code takes to execute as the package code adds a small time cost. |
Authors: | Joe Thorley [aut, cre] , Kirill Müller [aut] , Nadine Hussein [ctb] , Poisson Consulting [cph, fnd] |
Maintainer: | Joe Thorley <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.0.9001 |
Built: | 2024-12-12 01:24:06 UTC |
Source: | https://github.com/poissonconsulting/hmstimer |
A hms Timer is a hms::hms()
scalar which if
running has an attribute named start
that specifies the system time when the timer was started.
The elapsed time is the value of the scalar plus the difference between the current system time and the system time when the timer was started.
str(tmr_timer()) str(tmr_timer(1.5, start = TRUE)) x <- tmr_timer(1, start = TRUE) print(x) Sys.sleep(0.1) print(x) print(tmr_elapsed(x)) print(x)
str(tmr_timer()) str(tmr_timer(1.5, start = TRUE)) x <- tmr_timer(1, start = TRUE) print(x) Sys.sleep(0.1) print(x) print(tmr_elapsed(x)) print(x)
Called for the side effect of providing a message of the time required to execute the rest of the function.
local_timer(..., title = "", srcref = TRUE, .local_envir = rlang::caller_env())
local_timer(..., title = "", srcref = TRUE, .local_envir = rlang::caller_env())
... |
These dots are for future extensions and must be empty. |
title |
A string of the title. |
srcref |
A flag specifying whether to print the source reference. |
.local_envir |
The environment to use for scoping. |
fun <- function() { local_timer() Sys.sleep(0.1) 10 } fun()
fun <- function() { local_timer() Sys.sleep(0.1) 10 } fun()
Rounds a hms_timer()
up to the nearest second.
tmr_ceiling(x)
tmr_ceiling(x)
x |
A |
A hms_timer()
.
Other round:
tmr_floor()
,
tmr_format()
,
tmr_round()
tmr_ceiling(tmr_timer(18.9)) tmr_ceiling(tmr_timer(122.1))
tmr_ceiling(tmr_timer(18.9)) tmr_ceiling(tmr_timer(122.1))
Returns the elapsed time for a hms_timer()
as a hms_timer()
.
tmr_elapsed(x)
tmr_elapsed(x)
x |
A |
The elapsed time is the value of the scalar plus the difference between the current system time and the system time when the timer was started.
If the original hms_timer()
was running then the new hms_timer()
is assigned an attribute named start of the current system time.
A hms_timer()
of the elapsed time.
Other start_stop:
tmr_is_started()
,
tmr_is_stopped()
,
tmr_print()
,
tmr_reset()
,
tmr_start()
,
tmr_stop()
,
tmr_timer()
tmr <- tmr_start(tmr_timer()) print(tmr_elapsed(tmr)) Sys.sleep(0.01) print(tmr_elapsed(tmr)) tmr <- tmr_stop(tmr) print(tmr_elapsed(tmr)) Sys.sleep(0.01) print(tmr_elapsed(tmr))
tmr <- tmr_start(tmr_timer()) print(tmr_elapsed(tmr)) Sys.sleep(0.01) print(tmr_elapsed(tmr)) tmr <- tmr_stop(tmr) print(tmr_elapsed(tmr)) Sys.sleep(0.01) print(tmr_elapsed(tmr))
Rounds a hms_timer()
down to the nearest second.
tmr_floor(x)
tmr_floor(x)
x |
A |
A hms_timer()
.
Other round:
tmr_ceiling()
,
tmr_format()
,
tmr_round()
tmr_floor(tmr_timer(18.9)) tmr_floor(tmr_timer(122.1))
tmr_floor(tmr_timer(18.9)) tmr_floor(tmr_timer(122.1))
Converts a hms_timer()
to a string of the clock time
after rounding it to the number of digits.
tmr_format(x, digits = 3, ..., print_title = TRUE)
tmr_format(x, digits = 3, ..., print_title = TRUE)
x |
A |
digits |
A count of the number of decimal places. |
... |
These dots are for future extensions and must be empty. |
print_title |
A flag specifying whether to print the title. |
Negative values of digit are not permitted.
A character string.
Other round:
tmr_ceiling()
,
tmr_floor()
,
tmr_round()
tmr_format(tmr_timer(61.66)) tmr_format(tmr_timer(61.66), digits = 0)
tmr_format(tmr_timer(61.66)) tmr_format(tmr_timer(61.66), digits = 0)
Tests if a hms_timer()
is started (as indicated by the
presence of an attribute named start).
tmr_is_started(x)
tmr_is_started(x)
x |
A |
A flag (TRUE or FALSE).
Other start_stop:
tmr_elapsed()
,
tmr_is_stopped()
,
tmr_print()
,
tmr_reset()
,
tmr_start()
,
tmr_stop()
,
tmr_timer()
tmr <- tmr_timer(start = TRUE) print(tmr_is_started(tmr)) tmr <- tmr_stop(tmr) print(tmr_is_started(tmr))
tmr <- tmr_timer(start = TRUE) print(tmr_is_started(tmr)) tmr <- tmr_stop(tmr) print(tmr_is_started(tmr))
Tests if a hms_timer()
is stopped (as indicated by the
absence of an attribute named start).
tmr_is_stopped(x)
tmr_is_stopped(x)
x |
A |
A flag.
Other start_stop:
tmr_elapsed()
,
tmr_is_started()
,
tmr_print()
,
tmr_reset()
,
tmr_start()
,
tmr_stop()
,
tmr_timer()
tmr <- tmr_timer(start = TRUE) print(tmr_is_stopped(tmr)) tmr <- tmr_stop(tmr) print(tmr_is_stopped(tmr))
tmr <- tmr_timer(start = TRUE) print(tmr_is_stopped(tmr)) tmr <- tmr_stop(tmr) print(tmr_is_stopped(tmr))
Tests if a hms_timer()
has a title (as indicated by the
presence of an attribute named start).
tmr_is_titled(x)
tmr_is_titled(x)
x |
A |
A flag (TRUE or FALSE).
tmr_is_titled(tmr_timer()) tmr_is_titled(tmr_timer(title = "my timer"))
tmr_is_titled(tmr_timer()) tmr_is_titled(tmr_timer(title = "my timer"))
Returns the elapsed time for a hms_timer()
from the system time when the
timer was started and the current system time as an hms time.
tmr_print(x, ..., print_title = TRUE)
tmr_print(x, ..., print_title = TRUE)
x |
A |
... |
These dots are for future extensions and must be empty. |
print_title |
A flag specifying whether to print the title. |
The elapsed time is the value of the scalar plus the difference between the current system time and the system time when the timer was started.
A character string.
Other start_stop:
tmr_elapsed()
,
tmr_is_started()
,
tmr_is_stopped()
,
tmr_reset()
,
tmr_start()
,
tmr_stop()
,
tmr_timer()
x <- tmr_start(tmr_timer()) tmr_print(x)
x <- tmr_start(tmr_timer()) tmr_print(x)
Resets a hms_timer()
by creating a new one.
tmr_reset(x, seconds = 0)
tmr_reset(x, seconds = 0)
x |
A |
seconds |
A non-negative numeric scalar of the initial number of seconds. |
A hms_timer()
.
Other start_stop:
tmr_elapsed()
,
tmr_is_started()
,
tmr_is_stopped()
,
tmr_print()
,
tmr_start()
,
tmr_stop()
,
tmr_timer()
tmr <- tmr_timer(10) print(tmr) tmr_reset(tmr)
tmr <- tmr_timer(10) print(tmr) tmr_reset(tmr)
Rounds a hms_timer()
after updating it to the elapsed time.
tmr_round(x, digits = 0)
tmr_round(x, digits = 0)
x |
A |
digits |
A count of the number of decimal places. |
Negative values of digit are permitted.
A hms_timer()
.
Other round:
tmr_ceiling()
,
tmr_floor()
,
tmr_format()
tmr_round(tmr_timer(18.9)) tmr_round(tmr_timer(18.9), 1) tmr_round(tmr_timer(18.9), -1) tmr_round(tmr_timer(121), -2) # 121 is rounded to 100 seconds
tmr_round(tmr_timer(18.9)) tmr_round(tmr_timer(18.9), 1) tmr_round(tmr_timer(18.9), -1) tmr_round(tmr_timer(121), -2) # 121 is rounded to 100 seconds
Starts a hms_timer()
by adding an attribute named
start of the current system time.
tmr_start(x, ..., title = NULL)
tmr_start(x, ..., title = NULL)
x |
A |
... |
These dots are for future extensions and must be empty. |
title |
A string of the title. |
If the hms_timer()
is already started, the function
simply issues a warning and returns the original object.
A started hms_timer()
.
Other start_stop:
tmr_elapsed()
,
tmr_is_started()
,
tmr_is_stopped()
,
tmr_print()
,
tmr_reset()
,
tmr_stop()
,
tmr_timer()
tmr <- tmr_start(tmr_timer()) print(tmr_elapsed(tmr)) Sys.sleep(0.01) print(tmr_elapsed(tmr))
tmr <- tmr_start(tmr_timer()) print(tmr_elapsed(tmr)) Sys.sleep(0.01) print(tmr_elapsed(tmr))
Stops a hms_timer()
after updating it to the elapsed time.
tmr_stop(x)
tmr_stop(x)
x |
A |
If the hms_timer()
is already stopped, the function
simply issues a warning and returns the original object.
A stopped hms_timer()
.
Other start_stop:
tmr_elapsed()
,
tmr_is_started()
,
tmr_is_stopped()
,
tmr_print()
,
tmr_reset()
,
tmr_start()
,
tmr_timer()
tmr <- tmr_stop(tmr_timer(start = TRUE)) print(tmr_elapsed(tmr)) Sys.sleep(0.01) print(tmr_elapsed(tmr))
tmr <- tmr_stop(tmr_timer(start = TRUE)) print(tmr_elapsed(tmr)) Sys.sleep(0.01) print(tmr_elapsed(tmr))
Creates a hms_timer()
.
tmr_timer(seconds = 0, start = FALSE, ..., title = "")
tmr_timer(seconds = 0, start = FALSE, ..., title = "")
seconds |
A non-negative numeric scalar of the initial number of seconds. |
start |
A flag specifying whether to start the timer. |
... |
These dots are for future extensions and must be empty. |
title |
A string of the title. |
A hms_timer()
.
Other start_stop:
tmr_elapsed()
,
tmr_is_started()
,
tmr_is_stopped()
,
tmr_print()
,
tmr_reset()
,
tmr_start()
,
tmr_stop()
tmr_timer() tmr_timer(1, start = TRUE, title = "my timer") class(tmr_timer(2)) str(tmr_timer(2, start = TRUE, title = "a timer"))
tmr_timer() tmr_timer(1, start = TRUE, title = "my timer") class(tmr_timer(2)) str(tmr_timer(2, start = TRUE, title = "a timer"))
Returns a flag (character vector) of the title.
tmr_title(x)
tmr_title(x)
x |
A |
A flag of the title.
tmr_title(tmr_timer()) tmr_title(tmr_timer(title = "")) tmr_title(tmr_timer(title = "A Title"))
tmr_title(tmr_timer()) tmr_title(tmr_timer(title = "")) tmr_title(tmr_timer(title = "A Title"))
Sets the title of a hms_timer()
.
tmr_title(x) <- value
tmr_title(x) <- value
x |
A |
value |
A string of the title. |
A copy of the hms_timer()
with the new title.
tmr <- tmr_timer(title = "A title") tmr_print(tmr) tmr_title(tmr) <- "A different title" tmr_print(tmr) tmr_title(tmr) <- NULL tmr_print(tmr)
tmr <- tmr_timer(title = "A title") tmr_print(tmr) tmr_title(tmr) <- "A different title" tmr_print(tmr) tmr_title(tmr) <- NULL tmr_print(tmr)
With Timer
with_timer(code, ..., title = FALSE, srcref = FALSE)
with_timer(code, ..., title = FALSE, srcref = FALSE)
code |
A line or block of R code. |
... |
These dots are for future extensions and must be empty. |
title |
A flag specifying whether to add a title based on code. |
srcref |
A flag specifying whether to print the source reference. |
The result of executing the code.
fun <- function() { Sys.sleep(0.1) 10 } with_timer(fun()) with_timer({ for (i in 1:2) { Sys.sleep(0.1) } 20 })
fun <- function() { Sys.sleep(0.1) 10 } with_timer(fun()) with_timer({ for (i in 1:2) { Sys.sleep(0.1) } 20 })