如何使purrr map函数运行得更快?

编程入门 行业动态 更新时间:2024-10-26 20:26:12
本文介绍了如何使purrr map函数运行得更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 purrr 库中的 map 函数来应用 segmented 函数(来自 segmented 库),如下所示:

I am using map function from purrr library to apply segmented function (from segmented library) as follows:

library(purrr) library(dplyr) library(segmented) # Data frame is nested to create list column by_veh28_101 <- df101 %>% filter(LCType=="CFonly", Lane %in% c(1,2,3)) %>% group_by(Vehicle.ID2) %>% nest() %>% ungroup() # Functions: segf2 <- function(df){ try(segmented(lm(svel ~ Time, data=df), seg.Z = ~Time, psi = list(Time = df$Time[which(df$dssvel != 0)]), control = seg.control(seed=2)), silent=TRUE) } segf2p <- function(df){ try(segmented(lm(PrecVehVel ~ Time, data=df), seg.Z = ~Time, psi = list(Time = df$Time[which(df$dspsvel != 0)]), control = seg.control(seed=2)), silent=TRUE) } # map function: models8_101 <- by_veh28_101 %>% mutate(segs = map(data, segf2), segsp = map(data, segf2p))

对象 by_veh28_101 包含2457个小标题。最后一步是使用 map 函数,需要16分钟才能完成。有什么方法可以使它更快?

The object by_veh28_101 contains 2457 tibbles. And the last step, where map function is used, takes 16 minutes to complete. Is there any way to make this faster?

推荐答案

您可以使用函数 future_map 而不是地图。

You may use the function future_map instead of map.

此函数来自软件包 furrr ,它是 map 家庭。这是该软件包的自述的链接。

This function comes from the package furrr and is a parallel option for the map family. Here is the link for the README of the package.

由于您的代码问题不可复制,因此我无法在 map 和 future_map之间准备基准测试函数。

Because your code question it is not reproducible, I cant prepare a benchmark between the map and future_map functions.

具有 future_map 函数的代码如下:

library(tidyverse) library(segmented) library(furrr) # Data frame stuff.... # Your functions.... # future_map function # this distribute over the different cores of your computer # You set a "plan" for how the code should run. The easiest is `multiprocess` # On Mac this picks plan(multicore) and on Windows this picks plan(multisession) plan(strategy = multiprocess) models8_101 <- by_veh28_101 %>% mutate(segs = future_map(data, segf2), segsp = future_map(data, segf2p))

更多推荐

如何使purrr map函数运行得更快?

本文发布于:2023-11-30 18:03:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:更快   函数   purrr   map

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!