generated from r4ds/bookclub-template
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path23_Measuring_performance.Rmd
187 lines (117 loc) · 3.54 KB
/
23_Measuring_performance.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Measuring performance
**Learning objectives:**
- Understand how to improve your code for making it faster
- Learn what are the tools for improving your code
- Test how to profile your code
## Introduction
> "Before you can make your code faster, you first need to figure out what’s making it slow."
```{r echo=FALSE, fig.align='center',fig.cap="SLOW DOWN TO LEARN HOW TO CODE FASTER | credits: packtpub.com"}
knitr::include_graphics("images/23_code_faster.jpeg")
```
- **profile** your code: measure the run-time of each line of code using realistic inputs
- **experiment** with alternatives to find faster code
- **microbenchmark** to measure the difference in performance.
## Profiling
```{r message=FALSE, warning=FALSE, paged.print=FALSE}
library(profvis)
library(bench)
```
The tool to use is a **profiler**, it allows for **sampling** the code performance through stopping the execution of code every few milliseconds and recording all the steps.
Example:
```{r}
f <- function() {
pause(0.1)
g()
h()
}
g <- function() {
pause(0.1)
h()
}
h <- function() {
pause(0.1)
}
```
Profile the execution of f():
profvis::pause() is used instead of Sys.sleep()
profile f(), with utils::Rprof()
```{r}
tmp <- tempfile()
Rprof(tmp, interval = 0.1)
f()
Rprof(NULL)
writeLines(readLines(tmp))
```
**Visualising profiles**
Makes easier to build up a mental model of what you need to change:
profvis::profvis()
utils::summaryRprof()
```{r}
source("scripts/profiling-example.R")
profvis(f())
```
**Memory profiling and the garbage collector**
Profiling a loop that modifies an existing variable:
```{r}
profvis::profvis({
x <- integer()
for (i in 1:1e4) {
x <- c(x, i)
}
})
```
You can figure out what is the source of the problem by looking at the memory column. In this case, **copy-on-modify** acts in each iteration of the loop creating another copy of x.
**Limitations**
- Profiling does not extend to C code
- Anonymous functions are hard to figure out
- Arguments are evaluated inside another function
### Exercise
```{r eval=FALSE}
profvis::profvis({
f <- function(n = 1e5) {
x <- rep(1, n)
rm(x)
}
},torture = TRUE)
```
?rm()
[solution](https://advanced-r-solutions.rbind.io/measuring-performance.html)
## Microbenchmarking
*Measurement of the performance of a very small piece of code* is useful for comparing small snippets of code for specific tasks.
```{r echo=FALSE, fig.align='center',fig.cap = "Credits: Google search-engine"}
knitr::include_graphics("images/23_microbenchmarking.jpeg")
```
The {bench} package uses a high precision time.
bench::mark()
```{r}
library(bench)
x <- runif(100)
(lb <- bench::mark(
sqrt(x),
x ^ 0.5
))
```
- heavily right-skewed distribution
```{r}
require(ggbeeswarm)
plot(lb)
```
## Resources
- [profvis package](https://rstudio.github.io/profvis/)
- [bench package](https://cran.r-project.org/web/packages/bench/bench.pdf)
- [solutions](https://advanced-r-solutions.rbind.io/measuring-performance.html)
## Meeting Videos
### Cohort 1
(no video)
### Cohort 2
`r knitr::include_url("https://www.youtube.com/embed/_zeLDufwTwY")`
### Cohort 3
`r knitr::include_url("https://www.youtube.com/embed/Jdb00nepeWQ")`
### Cohort 4
`r knitr::include_url("https://www.youtube.com/embed/sCso4FAF1DE")`
### Cohort 5
`r knitr::include_url("https://www.youtube.com/embed/pOaiDK7J7EE")`
### Cohort 6
`r knitr::include_url("https://www.youtube.com/embed/d_pzz_AsoRQ")`
### Cohort 7
`r knitr::include_url("https://www.youtube.com/embed/4hngR1c9oP4")`