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.

Reply via email to