Using memoise to cache R values

April 12, 2014

(This article was first published on Dan Kelley Blog/R, and kindly contributed to R-bloggers)


The memoise package can be very handy for caching the results of slow calculations. In interactive work, the slowest calculations can be reading data, so that is demonstrated here. The microbenchmark package shows timing results.

Methods and results


First, load the package being tested, and also a benchmarking package.


Test conventional function

The demonstration will be for reading a CTD file.

## Loading required package: methods
## Loading required package: mapproj
## Loading required package: maps
## Loading required package: ncdf4
## Loading required package: tiff
microbenchmark(d <- read.oce("/data/arctic/beaufort/2012/d201211_0002.cnv"))
## Unit: milliseconds
##                                                          expr   min    lq
##  d <- read.oce("/data/arctic/beaufort/2012/d201211_0002.cnv") 160.4 162.5
##  median    uq   max neval
##   162.9 167.6 258.6   100

Memoise the function

Memoising read.oce() is simple

r <- memoise(read.oce)

Measure the speed of memoised code

microbenchmark(d <- r("/data/arctic/beaufort/2012/d201211_0002.cnv"))
## Unit: microseconds
##                                                   expr   min    lq median
##  d <- r("/data/arctic/beaufort/2012/d201211_0002.cnv") 47.47 48.61   49.5
##     uq    max neval
##  52.57 165199   100


In this example, the speedup was by a factor of about 3000.

The operation tested here is quick enough for interactive work, but this is a 1-dbar file, and the time would be increased to several seconds for raw CTD data, and increased to perhaps a half minute or so if a whole section of CTD profiles is to be read. Using memoise() would reduce that half minute to a hundredth of a second – easily converting an annoyingly slow operation to what feels like zero time in an interactive session.


To leave a comment for the author, please follow the link and comment on their blog: Dan Kelley Blog/R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)