Useful mainly in a script file
to time, then log, each step (or process
)
in the file.
timer(
process,
notes = NULL,
file = NULL,
name = NULL,
log = NULL,
time_df = NULL,
write_log = grepl("end", process)
)
Character. Name of the process being timed.
Character. Any notes associated with the process.
Character. Name of the (script) file.
Character. Might be the same as process
, or an element over
which process
is being run. e.g. name
could be a species name where the
same function is being run over many different species.
Character. Path to write the log file.
Object name or null for the initiation of a timer.
Write the log after this process. Default is only where process contains 'end'.
A dataframe (time_df), possibly with an additional row for the
current process. If write_log
then log
file is written.
Timer can be re-started by including 'start' in a process.
time_df <- timer("start script", file = "file 01", name = "example", log = "example.log")
Sys.sleep(1)
time_df <- timer("process 01", time_df = time_df)
Sys.sleep(1)
time_df
#> # A tibble: 2 × 7
#> name file process time notes elapsed log
#> <chr> <chr> <chr> <dttm> <chr> <time> <chr>
#> 1 example file 01 start script 2025-01-17 14:52:39 NA 00'00.000000" example.…
#> 2 example file 01 process 01 2025-01-17 14:52:40 NA 00'01.030296" example.…
time_df <- timer("end script", time_df = time_df) # log.log written
Sys.sleep(1)
time_df <- timer("start script", file = "file 02", time_df = time_df)
Sys.sleep(1)
time_df <- timer("process 01", time_df = time_df, write_log = TRUE) # log.log written
time_df <- timer("end script", time_df = time_df) # log.log written
# clean up
rm(time_df)
unlink("example.log")