Skip to contents

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 R6Class generator object.

Methods


Method is_running()

Usage

Timer$is_running()


Method elapsed()

Usage

Timer$elapsed()


Method reset()

Usage

Timer$reset()


Method start()

Usage

Timer$start()


Method stop()

Usage

Timer$stop()


Method clone()

The objects of this class are cloneable with this method.

Usage

Timer$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

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"