我有一个tibble,其中包含一个列表列,其中包含向量。我想创建一个新列来说明每个向量的长度。由于这个数据集很大(3M 行),我想使用该包来减少一些处理时间furrr。不过,看起来purrr比 更快furrr。怎么会?
为了演示这个问题,我首先模拟一些数据。不要费心去理解模拟部分的代码,因为它与问题无关。
数据模拟功能
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)
)
}
Run Code Online (Sandbox Code Playgroud)
模拟数据
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
Run Code Online (Sandbox Code Playgroud)
实际的问题
是我想改变一个新列(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
Run Code Online (Sandbox Code Playgroud)
我知道tictoc这不是最准确的基准测试方法,但仍然furrr应该更快(正如小插图所暗示的那样),但事实并非如此。我已确保数据未分组,因为作者解释说这furrr不适用于分组数据。那么还有什么其他解释可以比furrr慢(或不是很快)呢purrr?
编辑
我在github repo上发现了这个问题,它讨论了几乎相同的问题。furrr然而,情况有所不同。在github问题中,被映射的函数是用户定义的函数,需要附加额外的包。所以作者解释说,每个furrr工人在进行计算之前都必须附加所需的包。相比之下,我length()从 映射函数base R,因此实际上不应该有附加任何包的开销。
此外,作者认为可能会出现问题,因为plan(multisession)没有在 RStudio 中工作。但是将parallelly包更新到开发版本可以解决这个问题。
remotes::install_github("HenrikBengtsson/parallelly", ref="develop")
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个更新对我的情况没有任何影响。
正如我在对原始帖子的评论中所争论的那样,我怀疑工作人员分发非常大的数据集会产生开销。
为了证实我的怀疑,我使用了OP使用的相同代码,并进行了一次修改:我添加了延迟,0.000001结果是:purrr --> 192.45 sec和furrr: 44.707 sec(8 workers)。所花费的时间furrr仅为 1/4 purrr,距离 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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
609 次 |
| 最近记录: |