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.
library(timeR)
# 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.01 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.02 seconds elapsed.
# retrieve the table for all recordings
getTimer(my_timer)
#> event start end timeElapsed
#> 1 event one 2025-03-09 03:25:39.666373 2025-03-09 03:25:39.676405 0.01003194
#> 2 event two 2025-03-09 03:25:39.675314 2025-03-09 03:25:39.696245 0.02093101
#> comment
#> 1 <NA>
#> 2 my comment
# or create a timer object and setting verbose to false
my_timer2 <- createTimer(verbose = F)
# toggle on/off verbose
my_timer$toggleVerbose()
#> 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.
timeR
#initialize a timer object named mytimer: s, ms, us can be used as precision
mytimer <- createTimer(precision = "us")
# event 1
mytimer$start("event 1")
#> Warning in as.character.POSIXt(lubridate::now(), format = "%Y-%m%-%d
#> %H:%M:%OS6"): as.character(td, ..) no longer obeys a 'format' argument; use
#> format(td, ..) ?
# do something here
Sys.sleep(1)
mytimer$stop("event 1")
#> Warning in as.character.POSIXt(lubridate::now(), format = "%Y-%m%-%d
#> %H:%M:%OS6"): as.character(td, ..) no longer obeys a 'format' argument; use
#> format(td, ..) ?
#> Event: 'event 1' already has a record. Overwriting previous one.
#>
#> For event: 'event 1', 1 seconds elapsed.
#event 2
mytimer$start("event 2")
#> Warning in as.character.POSIXt(lubridate::now(), format = "%Y-%m%-%d
#> %H:%M:%OS6"): as.character(td, ..) no longer obeys a 'format' argument; use
#> format(td, ..) ?
# do something here
Sys.sleep(1)
mytimer$stop("event 2",comment = "custom comment")
#> Warning in as.character.POSIXt(lubridate::now(), format = "%Y-%m%-%d
#> %H:%M:%OS6"): as.character(td, ..) no longer obeys a 'format' argument; use
#> format(td, ..) ?
#> Event: 'event 2' already has a record. Overwriting previous one.
#>
#> For event: 'event 2', 1 seconds elapsed.
# print records
getTimer(mytimer)
#> event start end timeElapsed
#> 1 event 1 2025-03-09 03:25:39.732285 2025-03-09 03:25:40.73527 1.002985
#> 2 event 2 2025-03-09 03:25:40.741727 2025-03-09 03:25:41.744738 1.003011
#> comment
#> 1 <NA>
#> 2 custom comment
# get attributes for selected events
mytimer$getStartTime("event 1")
#> [1] "2025-03-09 03:25:39.732285"
mytimer$getStopTime("event 1")
#> NULL
mytimer$getTimeElapsed("event 1")
#> [1] 1.002985
mytimer$getComment("event 1")
#> [1] NA
mytimer$getEvent("event 1")
#> event start end timeElapsed
#> 1 event 1 2025-03-09 03:25:39.732285 2025-03-09 03:25:40.73527 1.002985
#> comment
#> 1 <NA>
#initalize a dataframe to store the information
timer_df = data.frame(matrix(ncol = 5, nrow = 0))
colnames(timer_df) <- c("event","start","end","timeElapsed","comment")
#event 1
t1 <- Sys.time()
# do something here
Sys.sleep(1)
t2 <- Sys.time()
timer_df <- rbind(timer_df,
data.frame(start = t1,
end = t2,
event = "event 1",
timeElapsed = t2-t1,
comment=NA))
#event 2
t1 <- Sys.time()
# do something here
Sys.sleep(1)
t2 <- Sys.time()
timer_df <- rbind(timer_df,
data.frame(start = t1,
end = t2,
event = "event 2",
timeElapsed = t2-t1,
comment = "custom comment"))
# print records
timer_df
#> start end event timeElapsed comment
#> 1 2025-03-09 03:25:41 2025-03-09 03:25:42 event 1 1.002118 secs <NA>
#> 2 2025-03-09 03:25:42 2025-03-09 03:25:43 event 2 1.002208 secs custom comment