Use R code profiling functions to improve performance

Applies to: SQL Server 2016 (13.x) and later versions

This article describes performance tools provided by R packages to get information about internal function calls. You can use this information to improve the performance of your code.

Tip

This article provides basic resources to get you started. For expert guidance, we recommend the Performance section in "Advanced R" by Hadley Wickham.

Use RPROF

rprof is a function included in the base package utils, which is loaded by default.

In general, the rprof function works by writing out the call stack to a file, at specified intervals. You can then use the summaryRprof function to process the output file. One advantage of rprof is that it performs sampling, thus lessening the performance load from monitoring.

To use R profiling in your code, you call this function and specify its parameters, including the name of the location where the log file is written. Profiling can be turned on and off in your code. The following syntax illustrates basic usage:

# Specify profiling output file.
varOutputFile <- "C:/TEMP/run001.log")
Rprof(varOutputFile)

# Turn off profiling
Rprof(NULL)
    
# Restart profiling
Rprof(append=TRUE)

Note

Using this function requires that Windows Perl be installed on the computer where code is run. Therefore, we recommend that you profile code during development in an R environment, and then deploy the debugged code to SQL Server.

R System Functions

The R language includes many base package functions for returning the contents of system variables. For example, as part of your R code, you might use Sys.timezone to get the current time zone, or Sys.Time to get the system time from R.

To get information about individual R system functions, type the function name as the argument to the R help() function from an R command prompt.

help("Sys.time")

Debug and Profiling in R

The documentation for Microsoft R Open, which is installed by default, includes a manual on developing extensions for the R language that discusses profiling and debugging in detail.

Next steps