-
-
Notifications
You must be signed in to change notification settings - Fork 71
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
Avoid double-loop and branching for leap years #430
Conversation
Locally I added a library(microbenchmark)
soy=xts:::startOfYear
# Comparing defaults: ~10% faster
microbenchmark(times=1e6, fast=soy(fast=TRUE), slow=soy(fast=FALSE))
# Unit: microseconds
# expr min lq mean median uq max neval
# fast 2.411 4.142 5.498975 4.251 4.421 55194.07 1e+06
# slow 2.571 4.621 5.851015 4.731 4.911 13580.37 1e+06
# Comparing huge edge case: ~40% faster
microbenchmark(times=1e4, fast=soy(from=0,to=1e4, fast=TRUE), slow=soy(from=0, to=1e4, fast=FALSE))
# Unit: microseconds
# expr min lq mean median uq max neval
# fast 35.92 42.36 50.34491 43.301 44.471 12413.64 10000
# slow 48.62 57.82 72.48831 59.040 60.631 12848.52 10000 This internal routine is only called once, here, so the default comparison is the fairest: Line 373 in ae39d2b
|
Thanks for this and the prior PR! I agree that this one is better, mostly because it's simpler. Also, I wouldn't worry about the 'fast' parameter. The routine is only used in one function to find the week of the year, and the improvement is only a handful of microseconds. |
Sorry, to clarify, I only did that to make the benchmark simpler, no plan to add it to the PR. |
Oh, I misunderstood! Now I understand that your PR is ~40% faster than the current code. That's a nice added bonus to the simpler code! |
I think 10% is a fairer comparison since the function is only ever called with defaults as of now :) |
Thanks for your work on this! |
Supersedes #429.
Actually, we can avoid the variable-length array issue altogether. The old implementation uses
leap
to:nyear
loop to populate whether each year is leapIt's better to just count the number of days directly in a single loop. This will be slightly more memory-efficient, and should be more computationally efficient too.