A simple timer as an R6 class.
The timer has four functions: $start(), $stop(), $reset() and $elapsed().
The $elapsed() function returns the elapsed wall clock time (as opposed to CPU time)
as an object of class lubridate::Duration.
Format
An R6::R6Class generator object.
Examples
# instantiate a new timer
timer <- Timer$new()
# no time has elapsed because the timer has not started
timer$elapsed()
#> [1] "0s"
# start the timer
timer$start()
# get the time elapsed (as an object of class lubridate::Duration)
# time elapsed is increasing because the timer is still running
timer$elapsed()
#> [1] "0.000995397567749023s"
timer$elapsed()
#> [1] "0.00417971611022949s"
# stop the timer
timer$stop()
# time elapsed is now fixed
timer$elapsed()
#> [1] "0.00580167770385742s"
timer$elapsed()
#> [1] "0.00580167770385742s"
# because timer is an object of class R6 use the clone() function
# to make a copy
timer2 <- timer$clone()
# reset the timer
timer$reset()
timer$elapsed()
#> [1] "0s"
# timer2 is not reset
timer2$elapsed()
#> [1] "0.00580167770385742s"