Closed katrinabrock closed 3 weeks ago
I'm finishing up the code I told you about where langevin()
will now only be called for unique time-lags instead of being lazily recycled, so this function's call should become much less frequent on GPS data. I also made the time-lag sorting happen at a higher level before calling the Kalman filter over and over within the optimizer - this was the annoying part.
I will check if those functions are called on vectors in a couple of days.
I added a speedup for sinc/sinch as well, replacing Vectorize({if...else})
with ifelse
. This is a 5x speedup of these functions based on my tests (below), and sinch
is a strong contributor to overall slowness based on my profiling. If these functions are also taking single values, not vectors, we could speed up further by replacing with if...else..
without vectorize or ifelse. I didn't go this far because I think these functions are used with longer vectors at some point, so retained all existing functionality.
test result
Unit: microseconds
expr min lq mean median
run_over_inputs(orig_sinch, input) 391.899 718.2425 941.4729 951.4340
run_over_inputs(ifelse_sinch, input) 56.143 91.6640 142.1830 142.8185
uq max neval cld
1059.483 7372.384 100 a
181.906 437.511 100 b
Unit: microseconds
expr min lq
run_over_inputs(orig_sinch, input, precomputed_sinh) 671.441 817.0655
run_over_inputs(ifelse_sinch, input, precomputed_sinh) 99.245 118.8870
mean median uq max neval cld
853.2558 853.534 886.392 1270.305 100 a
138.8795 139.271 154.753 193.450 100 b
Thanks @katrinabrock
Switching out
ifelse
forif(...){...}else{...}
. Added test to ensure outputs are identical.Functionality
This works only if the input is of length 1 (so a single number, not a vector). This assumption seems to be met. I determined this by running the below snippet from the vignettes with
if(length(x) > 1) print(x)
added inside these functions. Looking for input on whether this is a thorough enough test or alternatively if there may be vector inputs todexp1
anddexp2
when they get called from within functions other thanctmm.fit
.Performance
I was thinking to take this a first piece to translate to CPP, but based on benchmarks, this version is faster than what I was able to do in cpp. Compared the current version (
orig_dexp2
), version in this PR (if_else_dexp2
), and a cpp version (cpp_dexp2
) with this script:Sample Results
Simple if else contained in this PR seems to be the winner.
I don't expect this to have a huge impact on the overall performance of the package. Using it more as a proof of concept in adding tests and benchmarking.