[R] very slow "memoise"

Martin Møller Skarbiniks Pedersen tr@xpl@yer @ending from gm@il@com
Thu Nov 15 13:44:05 CET 2018


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



More information about the R-help mailing list