Learning Objectives

Motivation

Profiling a Function

Notes

  • You cannot profile C/C++ code using {profvis}. You would have to use a C++ profiler like gperftools. More information can be found in Section 3.4 of Writing R Extensions.

  • Using anonymous functions will make the results of {profvis} confusing.

  • The results are are a statistical sample of your call stack (what is being run and its ancestors), so your results will differ. But this only matters for really fast functions, which aren’t the ones you care about.

Exercise

Here is a crappy implementation of fitting a linear model:

lmsuck <- function(x, y) {
  x <- cbind(1, x)
  betahat <- c(solve(t(x) %*% x) %*% t(x) %*% y)
  names(betahat) <- c("beta0", "beta1")
  fits <- c(x %*% betahat)
  resids <- y - fits
  sigma <- sqrt(sum(resids^2) / (nrow(x) - ncol(x)))
  return(list(betahat = betahat, 
              sigma = sigma, 
              fits = fits, 
              resids = resids, 
              x = x, 
              y = y))
}

It seems to give similar results:

lm_sout <- lmsuck(x = mtcars$wt, y = mtcars$mpg)
lm_g <- lm(mpg ~ wt, data = mtcars)

lm_sout$betahat
##  beta0  beta1 
## 37.285 -5.344
coef(lm_g)
## (Intercept)          wt 
##      37.285      -5.344
lm_sout$sigma
## [1] 3.046
sigma(lm_g)
## [1] 3.046

Profile lmsuck() on a large (\(n \geq 100000\)) simulated dataset and tell me what takes the longest.

Microbenchmarking

Exercise

For summing up the columns of a matrix, I can think of at least four ways

  1. colSums()
  2. Matrix multiplication on the left by a vector of 1’s
  3. Using a for-loop on the columns, calculating the sum each time.
  4. Using functional programming with apply().

Create functions that implements all four of these approaches and tell me which one is the fastest for \(100\times 100\), \(1000\times 100\), and \(100\times 1000\) matrices.

New Functions

References

Jackson, M. A. 1975. Principles of Program Design. A.P.I.C. Studies in Data Processing. Academic Press. https://books.google.com/books?id=QdImAAAAMAAJ.

National Science Foundation Logo American University Logo Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.