Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Shortcut for count() #6860

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

WIP: Shortcut for count() #6860

wants to merge 1 commit into from

Conversation

krlmlr
Copy link
Member

@krlmlr krlmlr commented May 29, 2023

This is an attempt to improve the run time of TPCH-21 which uses count() over a large table with many groups. Gives a speedup of up to 3. A slippery slope, but might be worthwhile because the vast majority of summaries are simple counts?

This PR

options(conflicts.policy = list(warn = FALSE))
library(tidyverse)

set.seed(20230529)

get_data <- function(N) {
  distr <- (trunc(rexp(N, 3)) + 1)
  id <- rep(seq_along(distr), distr)
  sec <- as.integer(trunc(runif(length(id), 1, 3)))
  tibble(id, sec)
}

data <- get_data(1e4)
system.time(count(data, id, sec))
#>    user  system elapsed 
#>   0.021   0.001   0.022
data <- get_data(1e5)
system.time(count(data, id, sec))
#>    user  system elapsed 
#>   0.107   0.006   0.113
data <- get_data(1e6)
system.time(count(data, id, sec))
#>    user  system elapsed 
#>   1.418   0.050   1.469
data <- get_data(1e7)
system.time(count(data, id, sec))
#>    user  system elapsed 
#>  13.391   0.367  13.763

get_data(40) |>
  filter(id >= 30)
#> # A tibble: 15 × 2
#>       id   sec
#>    <int> <int>
#>  1    30     2
#>  2    31     2
#>  3    32     1
#>  4    32     1
#>  5    33     2
#>  6    34     2
#>  7    35     2
#>  8    35     2
#>  9    35     1
#> 10    36     2
#> 11    37     2
#> 12    38     2
#> 13    38     1
#> 14    39     1
#> 15    40     2

Created on 2023-05-29 with reprex v2.0.2

v1.1.2

options(conflicts.policy = list(warn = FALSE))
library(tidyverse)

set.seed(20230529)

get_data <- function(N) {
  distr <- (trunc(rexp(N, 3)) + 1)
  id <- rep(seq_along(distr), distr)
  sec <- as.integer(trunc(runif(length(id), 1, 3)))
  tibble(id, sec)
}

data <- get_data(1e4)
system.time(count(data, id, sec))
#>    user  system elapsed 
#>   0.034   0.002   0.036
data <- get_data(1e5)
system.time(count(data, id, sec))
#>    user  system elapsed 
#>   0.276   0.007   0.283
data <- get_data(1e6)
system.time(count(data, id, sec))
#>    user  system elapsed 
#>   3.486   0.085   3.576
data <- get_data(1e7)
system.time(count(data, id, sec))
#>    user  system elapsed 
#>  38.666   0.708  39.530

Created on 2023-05-29 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant