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
.
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.000930547714233398s"
timer$elapsed()
#> [1] "0.00407147407531738s"
# stop the timer
timer$stop()
# time elapsed is now fixed
timer$elapsed()
#> [1] "0.00564002990722656s"
timer$elapsed()
#> [1] "0.00564002990722656s"
# 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.00564002990722656s"