Enable or disable profiling of the execution of R expressions.
1 2 3 4
The file to be used for recording the profiling results.
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?
logical: filter out intervening call frames of the call tree. See the filtering out call frames section.
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
seconds, to the file specified. Either the
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:
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.
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
primitive functions do not do so: specifically those which are
"special" (see the ‘R Internals’ manual
for more details).
Individual statements will be recorded in the profile log if
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.
Lazy evaluation makes the call stack more complex because intervening
call frames are created between the time arguments are applied to a
function, and the time they are effectively evaluated. When the call
stack is represented as a tree, these intervening frames appear as
sibling nodes. For instance, evaluating
try(EXPR) produces the
following call tree, at the time
EXPR gets evaluated:
1 2 3 4 5 6
Lines 2 to 5 are intervening call frames, the last of which finally
triggered evaluation of
TRUE simplifies the profiler output by removing all sibling
nodes of intervening frames.
The same kind of call frame filtering is applied with
frames. When you call
eval(), two frames are pushed on the
stack to ensure a continuity between frames. Say we have these
1 2 3
called() in its own environment, via
eval(). The latter is called indirectly through
evaluator(). The net effect of this code is identical to just
called() directly, without the intermediaries. However,
the full call stack looks like this:
1 2 3 4 5 6
When call frame filtering is turned on, the true calling environment
called() is looked up, and the filtered call stack looks like
1 2 3
1. calling() 5. \-called() 6. \-EXPR()
If the calling environment is not on the stack, the function called by
eval() becomes a root node. Say we have:
With call frame filtering we then get the following filtered call stack:
5. called() 6. \-EXPR()
Profiling is not available on all platforms. By default, support for profiling is compiled in if possible – configure R with --disable-R-profiling to change this.
As R profiling uses the same mechanisms as C profiling, the two
cannot be used together, so do not use
Rprof in an executable
built for C-level profiling.
filename can be a UTF-8-encoded filepath that cannot be translated to
the current locale.
The profiler interrupts R asynchronously, and it cannot allocate
memory to store results as it runs. This affects line profiling,
which needs to store an unknown number of file pathnames. The
bufsize arguments control the size of
pre-allocated buffers to hold these results: the former counts the
maximum number of paths, the latter counts the numbers of bytes in
them. If the profiler runs out of space it will skip recording the
line information for new files, and issue a warning when
Rprof(NULL) is called to finish profiling.
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.
Rprofmem for other ways to track
1 2 3 4 5 6 7 8 9 10 11
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.