-
Notifications
You must be signed in to change notification settings - Fork 991
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
warn or optimize usage of ifelse()
#2677
Comments
Just re-ran Ricardo's benchmark here and yes, it's still slow: Isn't this just another use case for implementing more flexible The multiple- |
I wrote an alternative to library(microbenchmark)
library(magrittr)
cnd <- sample(c(TRUE, FALSE), size = 100e3, replace = TRUE)
yes <- sample(letters, size = 100e3, replace = TRUE)
no <- sample(letters, size = 100e3, replace = TRUE)
na <- sample(letters, size = 100e3, replace = TRUE)
microbenchmark(dplyr = dplyr::if_else(cnd, yes, no, na), base = ifelse(cnd, yes, no),
hutils = hutils::if_else(cnd, yes, no, na),
check = my_check) %T>%
print %>%
autoplot
|
Hugh - that's fantastic! Would you consider proposing your |
Really? Haha I only wrote it because I liked There are a few limitations -- namely I'm not sure how it works on more exotic classes like |
It's really really worth pursuing -- yes. R is kinda famous for its slow Scale up the data size though so that the times are many seconds or minutes. As you know, I tend to dismiss any chart that is in milliseconds scale. One of the R manuals explains how to build R and run its tests. If you've done that already in your very first communication, it has a much better chance. |
Longer version with a 2 billion-length cnd etc.
While I never intended it to be faster when I wrote it, I'm confident Similar with |
Regarding the inclusion in base R, it seems a discussion on R-dev crops up every so often (e.g. |
We could optimize |
Corresponding PR #3800 was closed as waiting for some strong user request for this. Closing here now too. |
Not sure the status of
ifelse()
speed in R these days. Years ago it used to be really slow and I avoided it. Is that still the case?I've seen this idiom quite a bit in the wild :
under
verbose=TRUE
there's a message about the plonk. It's slow and RAM inefficient I assume (haven't tested).Better would be using i, as per #2676 :
Nested
ifelse(ifelse(ifelse()))
also quite common. Either detect and warn aboutifelse()
usage and point to a document about how to avoid, or optimize it automatically. And ififelse()
in base is still slow, see why and try to improve it there.The text was updated successfully, but these errors were encountered: