Rprof() is a built-in profiling function in the R Language that helps you analyze where your R code spends most of its time. It works by sampling the call stack at regular intervals to create a statistical profile of your code’s execution.
Table of Contents
Note that at each time interval (say every 0.02 seconds), the function Rprof:
- Records the current function call stack
- Writes this information to a (log) file
- Later, the user can analyze which functions were active more often.
Why do we need Rprof()?
If the R code is running unnecessarily slowly, a handy tool for finding the
- Monitoring: We will call
Rprof() to start the monitor, run the R code, and then call it again with aNULLargument to stop the monitoring. - Profiling R code: Profiling R code gives the chance to identify bottlenecks and pieces of code that need to be more efficiently implemented, just by changing one line of the code.
For example, consider you want to create a data frame as described below:
x = data.frame(a = variable1, b = variable2)
Let us convert the above line of code to
x = c(variable1, variable2)
This big reduction happened because this line of code was called several times during the execution of the function.
Using R Code Profiling Functions
- The
rprof()is a function included in the base packageutils, which is loaded by default. - To use R profiling in our code, we can call this function and specify its parameters, including the name of the location of the log file that will be written. See the help for
Rproffor further details. - Profiling can be turned on and off in your code.
Types of Time Measurements
There are two types of Profiling measurements in R:
- Self time: Time spent in the function itself
- Total time: Time spent in the function and all functions it calls
The example output structure of the function is
# "by.self" vs "by.total"
Function Self Time (%) Total Time (%)
slow_func() 70% 70%
optimized() 20% 90% ← includes time in child functions
helper() 10% 10%
Practical Example
The following is a simple example with comments that explains the use of profiling in R. This simple example will help the user to understand profiling functionality. The example below creates three functions. After that, R profiling is started, then the created functions are run, profiling is stopped, and then a summary of the profiling is obtained to analyze the functions’ performance.
# 1. Define some functions
fast_function <- function() {
Sys.sleep(0.1) # Fast operation
}
slow_function <- function() {
Sys.sleep(0.5) # Slow operation
}
nested_function <- function() {
fast_function()
slow_function()
for(i in 1:1000) {
# Some computation
sqrt(i) * log(i)
}
}
# 2. Start profiling
Rprof("demo_profile.out", interval = 0.01)
# 3. Run code
nested_function()
fast_function()
slow_function()
# 4. Stop profiling
Rprof(NULL)
# 5. Analyze results
summary <- summaryRprof("demo_profile.out")
print(summary)The above output summary shows:
by.total: Time spent in each function, including its childrenby.self: Time spent in the function itself (excluding children)sample.interval: Sampling interval usedsampling.time: Total profiling time
Memory Profiling Capability
By memory profiling, we mean getting the profile of memory usage:
# Enable the memory profiling
Rprof("memory.out", memory.profiling = TRUE)
# R Code
x <- rnorm(1e6) # Large allocation
y <- x * 2 # Another allocation
z <- y + 1 # Yet another
Rprof(NULL)
summaryRprof("memory.out", memory = "both")
## Output
$by.self
self.time self.pct total.time total.pct mem.total
"rnorm" 0.02 100 0.02 100 7.6
$by.total
total.time total.pct mem.total self.time self.pct
"rnorm" 0.02 100 7.6 0.02 100
$sample.interval
[1] 0.02
$sampling.time
[1] 0.02The memory profiling tracks the following:
- Vcells: Vector memory allocations
- Ncells: Non-vector memory allocations
- Memory duplication events.
When to use Rprof()?
The Rprof() is good for:
- Identifying slow functions in long-running code
- Finding performance bottlenecks
- Comparing different implementations
- Understanding call Hierarchies
However, using Profiling is not ideal for:
- Very short code (code that runs in less than 0.5 seconds)
- Line-by-line profiling within functions
- Real-time debugging
Summary
The Rprof() is R’s sampling profiler that helps answer:
- Which functions are taking the most time?
- Where should the user focus optimization efforts?
- How does the user function call hierarchy look
It is a diagnostic tool, not a solution: it tells the user what is slow, not how to fix it. For most of the users today, profvis (which uses Rprof internally) provides a more user-friendly interface with visualizations, but understanding it is valuable for understanding profiling fundamentals in the R language.
Learn Statistics and Data Analysis


