Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add milestone test #199

Merged
merged 2 commits into from
Feb 23, 2024
Merged

add milestone test #199

merged 2 commits into from
Feb 23, 2024

Conversation

LittleBeannie
Copy link
Collaborator

@LittleBeannie LittleBeannie commented Feb 21, 2024

Closes #197

@LittleBeannie LittleBeannie linked an issue Feb 21, 2024 that may be closed by this pull request
@LittleBeannie LittleBeannie marked this pull request as draft February 21, 2024 17:28
@LittleBeannie LittleBeannie self-assigned this Feb 21, 2024
@LittleBeannie LittleBeannie added the development New feature or request label Feb 21, 2024
@LittleBeannie LittleBeannie marked this pull request as ready for review February 21, 2024 18:42
Copy link
Collaborator

@nanxstats nanxstats left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced your ifelse() calls with vanilla if-else constructs:

  • In non-vectorized cases, ifelse() is often much slower than if-else because ifelse() simply does too many things. In this particular formula, if-else is 4x faster than ifelse().
  • You can also write such formulas in a concise way by upcasting the logical variables, although it is a tad bit slower than if-else in this case.
library(microbenchmark)

f_ifelse <- function(fit_res, na_col, na_exp) {
  ifelse(na_col, 0, fit_res$std.err[1])^2 + ifelse(na_exp, 0, fit_res$std.err[2])^2
}

f_if_else <- function(fit_res, na_col, na_exp) {
  term1 <- if (na_col) 0 else fit_res$std.err[1]
  term2 <- if (na_exp) 0 else fit_res$std.err[2]
  term1^2 + term2^2
}

f_upcast <- function(fit_res, na_col, na_exp) {
  ((!na_col) * fit_res$std.err[1])^2 + ((!na_exp) * fit_res$std.err[2])^2
}

set.seed(42)
fit_res <- list(std.err = rnorm(2))
na_col <- FALSE
na_exp <- TRUE

f_ifelse(fit_res, na_col, na_exp)
f_if_else(fit_res, na_col, na_exp)
f_upcast(fit_res, na_col, na_exp)

microbenchmark(
  `ifelse` = f_ifelse(fit_res, na_col, na_exp),
  `if-else` = f_if_else(fit_res, na_col, na_exp),
  `upcast` = f_upcast(fit_res, na_col, na_exp),
  times = 5e+5
)

#> Unit: nanoseconds
#>     expr  min   lq      mean median   uq     max neval cld
#>   ifelse 1312 1435 1715.1805   1476 1640 7033919 1e+05   a
#>  if-else  287  369  424.8617    369  369 1164113 1e+05   b
#>   upcast  328  410  493.1685    410  451 2044342 1e+05   b

@nanxstats nanxstats merged commit f9d7581 into main Feb 23, 2024
7 checks passed
@nanxstats nanxstats deleted the 197-add-milestone-test branch February 23, 2024 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add milestone test
2 participants