Skip to content

Commit 0a132e5

Browse files
committed
compute time elapsed for both fit_xy and fit and print
1 parent ae42617 commit 0a132e5

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

R/fit.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ check_xy_interface <- function(x, y, cl, model) {
368368
#' @export
369369
print.model_fit <- function(x, ...) {
370370
cat("parsnip model object\n\n")
371+
cat("Fit in: ", paste(format(unclass(x$elapsed), digits = 2),
372+
attr(x$elapsed, "units")))
371373

372374
if (inherits(x$fit, "try-error")) {
373375
cat("Model fit failed with error:\n", x$fit, "\n")

R/fit_helpers.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,19 @@ form_form <-
5252
spec = object
5353
)
5454

55+
start <- Sys.time()
56+
5557
res$fit <- eval_mod(
5658
fit_call,
5759
capture = control$verbosity == 0,
5860
catch = control$catch,
5961
env = env,
6062
...
6163
)
64+
65+
end <- Sys.time()
6266
res$preproc <- list(y_var = all.vars(env$formula[[2]]))
67+
res$elapsed <- end - start
6368
res
6469
}
6570

@@ -107,19 +112,25 @@ xy_xy <- function(object, env, control, target = "none", ...) {
107112

108113
res <- list(lvl = levels(env$y), spec = object)
109114

115+
start <- Sys.time()
116+
110117
res$fit <- eval_mod(
111118
fit_call,
112119
capture = control$verbosity == 0,
113120
catch = control$catch,
114121
env = env,
115122
...
116123
)
124+
125+
end <- Sys.time()
126+
117127
if (is.vector(env$y)) {
118128
y_name <- character(0)
119129
} else {
120130
y_name <- colnames(env$y)
121131
}
122132
res$preproc <- list(y_var = y_name)
133+
res$elapsed <- end - start
123134
res
124135
}
125136

tests/testthat/test_fit_interfaces.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,20 @@ test_that('unknown modes', {
7979
)
8080
})
8181

82+
test_that("elapsed time parsnip mods", {
83+
lm1 <-
84+
linear_reg() %>%
85+
set_engine("lm") %>%
86+
fit_xy(x = mtcars[, 2:4], y = mtcars$mpg)
87+
88+
lm2 <-
89+
linear_reg() %>%
90+
set_engine("lm") %>%
91+
fit(mpg ~ ., data = mtcars)
92+
93+
expect_output(print(lm1), "Fit in:")
94+
expect_output(print(lm2), "Fit in:")
95+
expect_true(!is.null(lm1$elapsed))
96+
expect_true(!is.null(lm2$elapsed))
97+
})
98+

0 commit comments

Comments
 (0)