utils (version 3.6.2)

Rprof: Enable Profiling of R's Execution


Enable or disable profiling of the execution of R expressions.


Rprof(filename = "Rprof.out", append = FALSE, interval = 0.02,
       memory.profiling = FALSE, gc.profiling = FALSE,
       line.profiling = FALSE, numfiles = 100L, bufsize = 10000L)



The file to be used for recording the profiling results. Set to NULL or "" to disable profiling.


logical: should the file be over-written or appended to?


real: time interval between samples.


logical: write memory use information to the file?


logical: record whether GC is running?


logical: write line locations to the file?

numfiles, bufsize

integers: line profiling memory allocation


Enabling profiling automatically disables any existing profiling to another or the same file.

Profiling works by writing out the call stack every interval seconds, to the file specified. Either the summaryRprof function or the wrapper script R CMD Rprof can be used to process the output file to produce a summary of the usage; use R CMD Rprof --help for usage information.

How time is measured varies by platform:

On Windows:

Exactly what the time interval measures is subtle: it is time that the R process is running and executing an R command. It is not however just CPU time, for if readline() is waiting for input, that counts (on Windows, but not on a Unix-alike).

Note that the timing interval cannot be too small, for the time spent in each profiling step is added to the interval. What is feasible is machine-dependent, but 10ms seemed as small as advisable on a 1GHz machine.

On Unix-alikes

it is the CPU time of the R process, so for example excludes time when R is waiting for input or for processes run by system to return.

Note that the timing interval cannot usefully be too small: once the timer goes off, the information is not recorded until the next timing click (probably in the range 1--10 ms).

Functions will only be recorded in the profile log if they put a context on the call stack (see sys.calls). Some primitive functions do not do so: specifically those which are of type "special" (see the ‘R Internals’ manual for more details).

Individual statements will be recorded in the profile log if line.profiling is TRUE, and if the code being executed was parsed with source references. See parse for a discussion of source references. By default the statement locations are not shown in summaryRprof, but see that help page for options to enable the display.

See Also

The chapter on “Tidying and profiling R code” in “Writing R Extensions” (see the doc/manual subdirectory of the R source tree).

summaryRprof to analyse the output file.

tracemem, Rprofmem for other ways to track memory use.


Run this code
## some code to be profiled
## some code NOT to be profiled
Rprof(append = TRUE)
## some code to be profiled
## Now post-process the output as described in Details
# }

Run the code above in your browser using DataLab