Yes, you have misunderstood what memoise is for.

First, it is for when you call your function with the same inputs frequently as 
part of your calling-level algorithm. For your iterative calculation you would 
have  a stuck (cycling) process if the same value of current were  to be 
revisited... ever.

Second, it is for when your nextstep function is very slow to compute, since 
the memoisation process is non-trivial... certainly it takes much more work to 
keep the previous results around than your example nextstep algorithm would 
take to simply recompute the answer.

On November 15, 2018 4:44:05 AM PST, "Martin Møller Skarbiniks Pedersen" 
<traxpla...@gmail.com> wrote:
>Hi,
>
>  I want to compute a lot of values and I have tried to use
>memoise::memoise to speed-up the computation.
>  However it is much slower using the memoised version.
>
>I guess I have misunderstood how to use the package memoise or the
>purpose of the package.
>
>The code takes more than 2 minutes to finish but if I remove the line:
>"nextstep <- memoise(nextstep)" the code runs in less than 1 second. I
>was expecting a
>total different result.
>
>Here are the code:
>
>library(memoise)
>
>nextstep <- function(num) {
>    if (num %% 2 == 0) {
>        return(num/2)
>    }
>    num*3+1
>}
>
>nextstep <- memoise(nextstep)
>
>for (idx in 1:1e4) {
>    steps <- 0
>    current <- idx
>    while (current != 1) {
>      steps <- steps + 1
>      current <- nextstep(current)
>    }
>    cat(idx,steps,"\n")
>}
>
>
>Regards
>Martin
>
>______________________________________________
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to