Code Profiling

Code Profiling

Rprof()

Rprof()

profvis()

profvis()

library("profvis")
profvis({
  data(movies, package = "ggplot2movies") # Load data
  movies = movies[movies$Comedy == 1,]
  plot(movies$year, movies$rating)
  model = loess(rating ~ year, data = movies) # loess regression line
  j = order(movies$year)
  lines(movies$year[j], model$fitted[j]) # Add line to the plot
})

Exercise: Monopoly

In Monopoly moving around the board is complicated by the fact that rolling a double (a pair of 1's, 2's, ..., 6's) is special:

Code

## current is a value between 1 & 40
df = data.frame(d1 = sample(seq(1, 6), 3, replace = TRUE),
                d2 = sample(seq(1, 6), 3, replace = TRUE))

df$Total = apply(df, 1, sum)
df$IsDouble = df$d1 == df$d2

if (df$IsDouble[1] & df$IsDouble[2] & df$IsDouble[3]) {
  current = 11#Go To Jail
} else if (df$IsDouble[1] & df$IsDouble[2]) {
  current = current + sum(df$Total[1:3])
} else if (df$IsDouble[1]) {
  current = current + sum(df$Total[1:2])
} else {
  current = current + df$Total[1]
}

Exercise: Monopoly

vignette("profiling", package = "efficientTutorial")


jr-packages/efficientTutorial documentation built on Feb. 16, 2020, 7:05 p.m.