microbenchmark
microbenchmark copied to clipboard
Better handling of repeated, identical expressions
If the same expression is passed into microbenchmark() twice, then the resulting error is not very helpful.
library(microbenchmark)
microbenchmark(1 + 1, 1 + 1)
## Error in `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels, :
factor level [2] is duplicated
(On older versions of R, you get the warning duplicated levels in factors are deprecated).
I can't think of a good reason why you would want to include the same expression twice, so this usually means a copy and paste error. Consequently, you should throw a human-readable error or warning before the expressions are evaluated, explaining the problem. Some thing like
The same expression, 1 + 1, appears more than once.