When I want to estimate the running time of a R code, I use the function system.time()
.
library(dplyr)
system.time({
Titanic %>%
as.data.frame() %>%
mutate(Dataset = 1) %>%
bind_rows(as.data.frame(Titanic)) %>%
mutate_all(funs(replace_na(., NA))) %>%
filter(Dataset != 1)
})
# utilisateur système écoulé
# 0.02 0.00 0.02
Question:
Is there a way to know the running time of each operations, operations between each pipe (the mutate
, then the bind_rows
, then the filter
, etc.) without running each one by one or without writing several system.time()
?
In this example it is not useful, but sometimes I received a long script, with a long running time, and I would like to identify which operations are the lowest.
I made some research but I didn't find something useful.
Call the tic() function, then place any R expression or code or function(), then end it with toc() function call. It will print the execution time of the sleep_func().
%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).
table function, the base R function is almost 4 times and the dplyr function is 3 times slower!
To execute one specific line: we simply click into the line we would like to execute and press the button with the green arrow ("execute current line or selection; or CTRL+ENTER ). RStudio will copy this line into the R console and execute the line (as if we would enter the command and press enter).
You might be interested into the %L>%
pipe from my package pipes :
# devtools::install_github("moodymudskipper/pipes")
library(pipes)
Titanic %L>%
as.data.frame() %L>%
mutate(Dataset = 1) %L>%
bind_rows(as.data.frame(Titanic)) %L>%
mutate_all(list(~replace_na(., NA))) %L>%
filter(Dataset != 1)
# as.data.frame(.) ~ 0.03 sec
# mutate(., Dataset = 1) ~ 0 sec
# bind_rows(., as.data.frame(Titanic)) ~ 0 sec
# mutate_all(., list(~replace_na(., NA))) ~ 0 sec
# filter(., Dataset != 1) ~ 0.03 sec
# [1] Class Sex Age Survived Freq Dataset
# <0 rows> (or 0-length row.names)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With