timeR Quick Start

Yifu Yan


timeR package creates a R6 class, which allows you to create a timer object to easily time your codes. Meanwhile, all records are saved to a data frame, so it’s easy to retrieve all the records for later use.

Timing codes is not difficult but can be very tedious. With timeR, you can save your energy on timing and put more effort on your analysis. You can use timeR to timing training time for machine learning models, record speed for requests hen running web-scraping codes or other situations that you need to keep records of time.

Basic Usage

# Create a timer object(precision default to s)
my_timer <- createTimer()

# start timing for an event
my_timer$start("event one")

#start timing for another event
my_timer$start("event two")

# stop timing for the events
my_timer$stop("event one")
#> Event: 'event one' already has a record. Overwriting previous one.
#> For event: 'event one', 0 seconds elapsed.
my_timer$stop("event two", comment = "my comment") # comment is optional
#> Event: 'event two' already has a record. Overwriting previous one.
#> For event: 'event two', 0 seconds elapsed.

# retrieve the table for all recordings
#>       event               start                 end timeElapsed    comment
#> 1 event one 2020-06-22 10:19:35 2020-06-22 10:19:35           0       <NA>
#> 2 event two 2020-06-22 10:19:35 2020-06-22 10:19:35           0 my comment

# or create a timer object and setting verbose to false
my_timer2 <- createTimer(verbose = F)

# toggle on/off verbose
#> Verbose set to: FALSE.

# warnings will still be shown when verbose is turned off
my_timer$stop("event one")


Let’s compare the workflow with and without timeR in the following scenario.

Our goal is to keep records of two events: event 1 and event 2.
We need to keep records of starting and stopping time for both events as well as calculate the time difference between them. Also, we want to save those information to a data frame for later analysis.

Timing Codes With timeR

Timing Codes Conventionally