Skip to content

Improve speed of group_by()#121

Draft
etiennebacher wants to merge 4 commits intonathaneastwood:masterfrom
etiennebacher:improve-groupby-speed
Draft

Improve speed of group_by()#121
etiennebacher wants to merge 4 commits intonathaneastwood:masterfrom
etiennebacher:improve-groupby-speed

Conversation

@etiennebacher
Copy link
Copy Markdown
Contributor

Related to #119.

Benchmark setup:

d <- data.frame(
  g1 = sample(LETTERS, 8000, TRUE),
  g2 = sample(LETTERS, 8000, TRUE),
  g3 = sample(LETTERS, 8000, TRUE),
  x1 = runif(8000),
  x2 = runif(8000),
  x3 = runif(8000)
)

# return a list of results so that both functions return the same output (without
# all the class problem, tibble vs data.frame)
poor = function() {
  foo <- d %>% 
    group_by(g1, g2, g3) |> 
    summarise(x1 = mean(x1), x2 = max(x2), x3 = min(x3))

  list(foo$x1, foo$x2, foo$x3)
}

dpl = function() {
  foo <- d %>% 
    dplyr::group_by(g1, g2, g3) |> 
    dplyr::summarise(x1 = mean(x1), x2 = max(x2), x3 = min(x3))

  list(foo$x1, foo$x2, foo$x3)
}

bench::mark(
  poor(),
  dpl()
)

Before:

#> # A tibble: 2 × 13
#>   expression      min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result     memory     time           gc      
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list>     <list>     <list>         <list>  
#> 1 poor()        11.6s    11.6s    0.0863    1.83GB    11.0      1   127      11.6s <list [3]> <Rprofmem> <bench_tm [1]> <tibble>
#> 2 dpl()       157.2ms  178.3ms    5.77      7.34MB     5.77     3     3    520.2ms <list [3]> <Rprofmem> <bench_tm [3]> <tibble>

After:

#> # A tibble: 2 × 13
#>   expression      min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result     memory     time           gc      
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list>     <list>     <list>         <list>  
#> 1 poor()        7.77s    7.77s     0.129    1.83GB     9.78     1    76      7.77s <list [3]> <Rprofmem> <bench_tm [1]> <tibble>
#> 2 dpl()      112.42ms 152.17ms     6.66     3.39MB     3.33     4     2   601.02ms <list [3]> <Rprofmem> <bench_tm [4]> <tibble>

@etiennebacher etiennebacher marked this pull request as draft January 6, 2023 09:40
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jan 6, 2023

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.60%. Comparing base (3cc0a99) to head (e6e28e8).
⚠️ Report is 7 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #121      +/-   ##
==========================================
+ Coverage   93.59%   93.60%   +0.01%     
==========================================
  Files          59       59              
  Lines        1483     1486       +3     
==========================================
+ Hits         1388     1391       +3     
  Misses         95       95              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@etiennebacher

This comment was marked as outdated.

@nathaneastwood
Copy link
Copy Markdown
Owner

I've been away. I had every intention of reviewing this this evening but I spent it catching up on other things. I'll try and look tomorrow after work.

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.

3 participants