Chapter 18Tuning and terminating methods

This chapter is about improving the performance of computational processes. For most calculations, I do not find it necessary to bother with measuring how fast my work is getting done. However, there are situations where a calculation must be performed many times and it is also slow enough to complete that needed results are not available quickly enough.

18.1 Timing and profiling

R offers the function system.time() for timing commands. In particular, we usually select the third element of the returned vector as the time taken by the command to execute. Here is an example where we time how long it takes to generate a set of random uniform numbers.

nnum <- 100000L
cat("Time to generate ", nnum, "  is ")
## Time to generate  100000   is
tt <- system.time(xx <- runif(nnum))[[3]]
cat(tt, "\n")
## 0.005

Besides the system.time facility, R offers some packages to assist in timing. Unfortunately, while the English word “benchmark” would be a logical choice, both “benchmark” and “benchmarking” refer to packages for other calculations. benchmark is about statistical learning methods (Eugster and Leisch, 2011; Eugster et al. 2012), and while it may have some features useful for timing R expressions, there are many of these features and most are not directed to the task at hand. Thus benchmark is not one of the tools I use. Similarly, package Benchmarking is about stochastic frontier analysis, a form of economic modeling.

Two packages that are useful ...

Get Nonlinear Parameter Optimization Using R Tools now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.