Skip to content

Instantly share code, notes, and snippets.

@szimmer
Created February 26, 2020 13:52
Show Gist options
  • Save szimmer/83211291262aa8f940dd860b90bb560e to your computer and use it in GitHub Desktop.
Save szimmer/83211291262aa8f940dd860b90bb560e to your computer and use it in GitHub Desktop.
Same operation done using: rowwise(), pmin(), pmap_dbl() via Mara Averick
library(tidyverse)
set.seed(406)
N <- 2000


library(rbenchmark)

benchmark(
  "rowwise"={
    df <- tibble(x=runif(N), y=runif(N), z=runif(N))
    df %>% rowwise() %>% mutate(m = min(c(x, y, z)))
    },
  "pmin"={
    df <- tibble(x=runif(N), y=runif(N), z=runif(N))
    df %>% mutate(m = pmin(x, y, z))
    },
  "pmap_dbl"={
    df <- tibble(x=runif(N), y=runif(N), z=runif(N))
    df %>% mutate(m = pmap_dbl(list(x, y, z), min))
    },
  replications=1000, 
  columns = 
    c("test", "replications", "elapsed", "relative", "user.self", "sys.self")
  )
#>       test replications elapsed relative user.self sys.self
#> 3 pmap_dbl         1000    8.11    7.651      7.92        0
#> 2     pmin         1000    1.06    1.000      1.03        0
#> 1  rowwise         1000    7.82    7.377      7.48        0

Created on 2020-02-26 by the reprex package (v0.3.0)

library(tidyverse)
set.seed(406)
N <- 2000
library(rbenchmark)
benchmark(
"rowwise"={
df <- tibble(x=runif(N), y=runif(N), z=runif(N))
df %>% rowwise() %>% mutate(m = min(c(x, y, z)))
},
"pmin"={
df <- tibble(x=runif(N), y=runif(N), z=runif(N))
df %>% mutate(m = pmin(x, y, z))
},
"pmap_dbl"={
df <- tibble(x=runif(N), y=runif(N), z=runif(N))
df %>% mutate(m = pmap_dbl(list(x, y, z), min))
},
replications=1000,
columns =
c("test", "replications", "elapsed", "relative", "user.self", "sys.self")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment