4

我有一个小标题,其中包含一个带有向量的列表列。我想创建一个新列来说明每个向量的长度。由于这个数据集很大(3M 行),我想用这个furrr包来减少一些处理时间。但是,它似乎purrrfurrr. 怎么来的?

为了演示这个问题,我首先模拟了一些数据。不要费心去理解模拟部分的代码,因为它与问题无关。


数据模拟功能

library(stringi)
library(rrapply)
library(tibble)

simulate_data <- function(nrows) {
  split_func <- function(x, n) {
    unname(split(x, rep_len(1:n, length(x))))
  }
  
  randomly_subset_vec <- function(x) {
    sample(x, sample(length(x), 1))
  }
  
  tibble::tibble(
    col_a = rrapply(object = split_func(
      x = setNames(1:(nrows * 5),
                   stringi::stri_rand_strings(nrows * 5,
                                              2)),
      n = nrows
    ),
    f      = randomly_subset_vec),
    col_b = runif(nrows)
  )
  
} 

模拟数据

set.seed(2021)

my_data <- simulate_data(3e6) # takes about 1 minute to run on my machine

my_data
## # A tibble: 3,000,000 x 2
##    col_a      col_b
##    <list>     <dbl>
##  1 <int [3]> 0.786 
##  2 <int [5]> 0.0199
##  3 <int [2]> 0.468 
##  4 <int [2]> 0.270 
##  5 <int [3]> 0.709 
##  6 <int [2]> 0.643 
##  7 <int [2]> 0.0837
##  8 <int [4]> 0.159 
##  9 <int [2]> 0.429 
## 10 <int [2]> 0.919 
## # ... with 2,999,990 more rows


我想改变一个新列( )的实际问题length_col_a,它将解释col_a. 我要这样做两次。先用purrr::map_int()再用furrr::future_map_int()

library(dplyr, warn.conflicts = T)
library(purrr)
library(furrr)
library(tictoc)

# first with purrr:
##################
tic()
my_data %>%
  mutate(length_col_a = map_int(.x = col_a, .f = ~length(.x)))

## # A tibble: 3,000,000 x 3
##    col_a      col_b length_col_a
##    <list>     <dbl>        <int>
##  1 <int [3]> 0.786             3
##  2 <int [5]> 0.0199            5
##  3 <int [2]> 0.468             2
##  4 <int [2]> 0.270             2
##  5 <int [3]> 0.709             3
##  6 <int [2]> 0.643             2
##  7 <int [2]> 0.0837            2
##  8 <int [4]> 0.159             4
##  9 <int [2]> 0.429             2
## 10 <int [2]> 0.919             2
## # ... with 2,999,990 more rows
toc()
## 6.16 sec elapsed


# and now with furrr:
####################
future::plan(future::multisession, workers = 2)

tic()
my_data %>%
  mutate(length_col_a = future_map_int(col_a, length))
## # A tibble: 3,000,000 x 3
##    col_a      col_b length_col_a
##    <list>     <dbl>        <int>
##  1 <int [3]> 0.786             3
##  2 <int [5]> 0.0199            5
##  3 <int [2]> 0.468             2
##  4 <int [2]> 0.270             2
##  5 <int [3]> 0.709             3
##  6 <int [2]> 0.643             2
##  7 <int [2]> 0.0837            2
##  8 <int [4]> 0.159             4
##  9 <int [2]> 0.429             2
## 10 <int [2]> 0.919             2
## # ... with 2,999,990 more rows
toc()
## 10.95 sec elapsed

我知道tictoc这不是最准确的基准测试方法,但仍然furrr应该更快(正如小插图所暗示的那样),但事实并非如此。我确保数据没有分组,因为作者解释说furrr不适用于分组数据。那么还有什么其他解释可以解释为furrr比 慢(或不是非常快)purrr


编辑


我在讨论几乎相同问题的 github repo上发现了这个问题。furrr但是,情况有所不同。在 github issue 中,被映射的函数是用户定义的函数,需要附加额外的包。所以作者解释说,每个furrr工人在进行计算之前都必须附上所需的包裹。相比之下,我length()从 映射函数base R,因此实际上应该没有附加任何包的开销。

此外,作者认为可能会出现问题,因为plan(multisession)没有在 RStudio 中工作。但是将parallelly包更新到开发版本可以解决这个问题。

remotes::install_github("HenrikBengtsson/parallelly", ref="develop")

不幸的是,此更新对我的情况没有任何影响。

4

1 回答 1

2

正如我在对原始帖子的评论中所说的那样,我怀疑工作人员分发非常大的数据集会导致开销。

为了证实我的怀疑,我使用了 OP 使用的相同代码,只进行了一次修改:我添加了延迟,0.000001结果是:purrr --> 192.45 secfurrr: 44.707 sec( 8 workers)。所用时间furrr仅为所用时间的 1 purrr/4——与 1/8 相差甚远!

根据 OP 的要求,我的代码如下:

library(stringi)
library(rrapply)
library(tibble)

simulate_data <- function(nrows) {
  split_func <- function(x, n) {
    unname(split(x, rep_len(1:n, length(x))))
  }
  
  randomly_subset_vec <- function(x) {
    sample(x, sample(length(x), 1))
  }
  
  tibble::tibble(
    col_a = rrapply(object = split_func(
      x = setNames(1:(nrows * 5),
                   stringi::stri_rand_strings(nrows * 5,
                                              2)),
      n = nrows
    ),
    f      = randomly_subset_vec),
    col_b = runif(nrows)
  )
  
} 

set.seed(2021)

my_data <- simulate_data(3e6) # takes about 1 minute to run on my machine

my_data

library(dplyr, warn.conflicts = T)
library(purrr)
library(furrr)
library(tictoc)

# first with purrr:
##################

######## ---->  DELAY <---- ########
f <- function(x) {Sys.sleep(0.000001); length(x)}

tic()
my_data %>%
  mutate(length_col_a = map_int(.x = col_a, .f = ~ f(.x)))
toc()

plan(multisession, workers = 8)

tic()
my_data %>%
  mutate(length_col_a = future_map_int(col_a, f))
toc()
于 2021-11-02T22:59:05.833 回答