13.3 Timing your code

As you can imagine the more iterations we have the better our parameter estimates will be. This comes with a tradeoff: it takes some time to run the full estimate. To get a sense of timing the code there is a helpful clock function in R that serves a stopwatch if you will. Let’s check this out with one iteration of the phosphorous dataset:

# This "starts" the stopwatch
start_time <- Sys.time()

# Compute a single mcmc estimate
phosphorous1_mcmc <- mcmc_estimate(
  model = phos_model,
  data = phosphorous,
  parameters = phos_param,
  iterations = 1
)

# End the stopwatch
end_time <- Sys.time()

# Determine the difference between the start and end times
end_time - start_time
## Time difference of 0.106024 secs

Timing the code for one iteration gives you a ballpark estimate for a full MCMC parameter estimate. If we were to run N MCMC iterations, a good benchmark would be to multiply the time difference (end_time - start_time) by N. Performance time varies by computer and the other programs / apps that are running at the same time. However, timing your code should give you an idea of what to expect.