[Rd] Code compilation: Drop certain statements in a function before calling it multiple times?

Henrik Bengtsson hb at biostat.ucsf.edu
Thu May 23 21:19:38 CEST 2013


Hi,

I make heavy use of verbose statements in my code, verbose output that
can be enabled/disabled via an argument.  Here is a dummy example:

foo <- function(n=10, verbose=FALSE) {
  res <- 0;
  for (k in 1:n) {
     if (verbose) cat("Iteration ", k, "...\n", sep="");
     res <- res + k;
     if (verbose) cat("Iteration ", k, "...done\n", sep="");  }
  }
  res;
}

Even with verbose=FALSE, one pay an noticeable overhead due to it when
calling foo(verbose=FALSE).  Thus, before calling it, i'd like to
pre-compile this function by dropping the verbose-related statements,
e.g.

if (verbose) {
  fooT <- dropVerbose(foo);
}

such that I basically get:

fooT <- function(n=10, verbose=FALSE) {
  res <- 0;
  for (k in 1:n) {
     res <- res + k;
  }
  res;
}

Just to clarify, the immediate use case for this is to compile local
functions, e.g.

bar <- function(..., verbose=FALSE) {
  foo <- function(...) { ... };
  if (verbose) {
    foo <- dropVerbose(foo);
  }
  foo(..., verbose=verbose);
}

Instead of me reinventing the wheel does anyone know of tools that
makes it easier to drop certain statements in existing functions?


RESULTS:
> t <- system.time(for (k in 1:1e5) foo());
> tT <- system.time(for (k in 1:1e5) fooT());
> tT/t
     user    system   elapsed
0.6635514       NaN 0.6605505

I am aware of the 'compiler' package, which is great, but as far as I
understand the above speed up when dropping statements still applies;

> fooC <- compiler::cmpfun(foo);
> fooTC <- compiler::cmpfun(fooT);
> tC <- system.time(for (k in 1:1e5) fooC());
> tTC <- system.time(for (k in 1:1e5) fooTC());
> tTC/tC
     user    system   elapsed
0.6521739       NaN 0.6400000


Thanks,

Henrik

PS. The same idea of compilation applies when you make heavy use of
assert statements in your development, sanity checks that are there to
make sure *your* coding is correct and that basically never fails but
you keep in just in case.  It would be nice to have an option to have
a Just-in-Time (JIT) options for dropping those assert statements,
e.g. the user runs through an analysis on some test data and confirms
everything works as it should and then launch a two-week jobs where
asserts have been dropped.



More information about the R-devel mailing list