Skip to content

Commit

Permalink
Updated code from week 4 workshop
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed Mar 20, 2024
1 parent 3c32221 commit ad07079
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions week4/workshop_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,29 @@ sq <- function(x) {
memo_sq <- memoise(sq)
memo_sq(2)
memo_sq(2)

## Vectorisation

calc_area <- function(N = 1e4) {
hits <- 0
N <- 1e3
for(i in seq(N)) {
u1 <- runif(1, 0,1)
u2 <- runif(1, 0,1)
if(u2 < u1^2)
hits <- hits + 1
}
hits/N
}

calc_area2 <- function(N = 1e4) {
u1 <- runif(N, 0, 1)
u2 <- runif(N, 0, 1)
mean(u2 < u1^2)
}

bench::mark(
calc_area(),
calc_area2(),
check = FALSE
)

0 comments on commit ad07079

Please sign in to comment.