查看原文
其他

do.call 比 Reduce 快?

JunJunLab 老俊俊的生信笔记 2022-08-15


蒲公英的约定

1引言

在处理大数据的时候,我们往往会使用 *apply 家族函数及多线程等等操作,对于循环的结果可以使用 Reduce 或者 do.call 来合并结果。

在处理数据的时候我发现 do.call 竟然比 Reduce 快了不少,可以节约大量时间!

2示例

使用 Reduce 合并结果:

options(future.globals.maxSize= 5000e6)

system.time({future_lapply(1:50000function(x){
  tmp <- center_df[x,]
  if(tmp$end5 >= tmp$st_pos & tmp$end3 <= tmp$sp_pos){
    pos_new = c(tmp$end5:tmp$end3)
    score = 1/tmp$len
    tmp_score <- data.table(type=tmp$type,rname=tmp$rname,pos_new,score)
    return(tmp_score)
  }else{}
}) %>% Reduce('rbind',.) %>%
    data.table() %>%
    .[,.(sum_density = sum(score)),by = .(type,rname,pos_new)] -> center_score_df})

用户  系统  流逝
82.35  1.24 83.52

使用 do.call 合并结果:

system.time({future_lapply(1:50000function(x){
  tmp <- center_df[x,]
  if(tmp$end5 >= tmp$st_pos & tmp$end3 <= tmp$sp_pos){
    pos_new = c(tmp$end5:tmp$end3)
    score = 1/tmp$len
    tmp_score <- data.table(type=tmp$type,rname=tmp$rname,pos_new,score)
    return(tmp_score)
  }else{}
}) %>% do.call('rbind',.) %>%
    data.table() %>%
    .[,.(sum_density = sum(score)),by = .(type,rname,pos_new)] -> center_score_df})

用户  系统  流逝
18.53  0.34 18.89

将近快了 4 倍多!见识了,一天一个小技巧!




  老俊俊生信交流群 ,QQ,


老俊俊微信:


知识星球:



今天的分享就到这里了,敬请期待下一篇!

最后欢迎大家分享转发,您的点赞是对我的鼓励肯定

如果觉得对您帮助很大,赏杯快乐水喝喝吧!



  





南京的春

tstrsplit 加速你的字符串分割

ggplot 绘制分半小提琴图+统计检验

Ribo-seq 可视化进阶

PCA 主成分分析详解

ggplot 绘制旋转的相关性图

Rsamtools 批量处理 bam 文件

GSEApy 做富集分析及可视化

pysam 读取 bam 文件准备 Ribo-seq 质控数据

sankeywheel 绘制唯美桑基图

◀...

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存