lineprof: Line profiling.

Description Usage Arguments Details Profiling output Navigation Display Examples

Description

lineprof uses R built-in time and memory profiler Rprof to collect line profiling data and displays it in ways that help you figure out where the bottlenecks in your code R.

Usage

1
lineprof(code, interval = 0.001, torture = FALSE)

Arguments

code

code to profile.

interval

interval, in seconds, between profile snapshots. R's timer has a resolution of at best 1 ms, so there's no reason to make this value smaller, but if you're profiling a long running function you might want to set it to something larger.

torture

if TRUE, turns on gctorture which forces gc to run after (almost) every allocation. This is useful if you want to see exactly when memory is released. It also makes R run extremely slowly (10-100x slower than usual) so it can also be useful to simulate a smaller interval.

Details

R's profiler is a sampling profiler, which means that it stops execution every interval seconds and records the current call stack. This means that it is somewhat random: if you run the profiler repeatedly on the same code you'll see slightly different outputs depending on exactly where the profiler stopped each time.

Profiling output

For each sequence of calls, lineprof calculates:

Note that memory allocation is only approximate due to the nature of the sampling profiler: if a large block of memory is allocated and then released in between ticks of the timer, no change in memory will be recorded. Using torture = TRUE helps prevent this, mostly by making R super slow.

Navigation

There are two ways to navigate through the line profiling output: using a shiny GUI, generated by shine; or through the command line, using focus.

Display

The default print method uses reduce_depth to reduce the call stack to show only two steps down, and then formats the output with format.lineprof. An alternative option is to use align, which when src refs are available, will align the profiling data with the original source code.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
source(find_ex("read-delim.r"))
source(find_ex("read-table.r")) # local copy so get line numbers
wine <- find_ex("wine.csv")

## Not run: 
x1 <- lineprof(read.table2(wine, sep = ","), torture = TRUE)
x2 <- lineprof(read_delim(wine), torture = TRUE)

shine(x1)
shine(x2)

## End(Not run)

hadley/lineprof documentation built on May 17, 2019, 10:42 a.m.