如何加速或并行化此 R 代码?

雅克斯科

这段代码运行良好,但速度有点慢。我注意到它只在处理器的一个核心上运行。如果它使用多个核心,它可能会快一点。

### proximity filter
options("scipen"=100)
library(geosphere)

# split up data into regions
splitdt<-split(geocities, geocities$airport_code)

## reduce cities
dat=geocities[FALSE,][]
currentregion=1

while (currentregion <= NROW(splitdt)){
    workingregion <- as.data.frame(splitdt[[currentregion]]) ## set region
    workingregion$remove = FALSE
    setDT(workingregion)
    #plot(workingregion$longitude,workingregion$latitude)
    currentorigin=1

    while (currentorigin <= NROW(workingregion)) {
        # choose which row to use
        # as the first part of the distance formula
        workingorigin <- workingregion[,c("longitude","latitude")] %>% slice(currentorigin) ## set LeadingRow city
        setDT(workingorigin)

        # calculate the distance from the specific row chosen
        # and only keep ones which are further than 20km
        workingregion<-workingregion %>% rowwise() %>% mutate(remove =
        ifelse(distHaversine(c(longitude, latitude), workingorigin) != 0 &  # keep workingorigin city
        distHaversine(c(longitude, latitude), workingorigin) < 17000,TRUE,workingregion$remove))

        # remove matched cities
        workingregion <- workingregion[workingregion$remove!=TRUE,]

        currentorigin = currentorigin+1
    }
    currentregion = currentregion+1
    # save results
    workingregion <- workingregion[workingregion$remove!=TRUE,]
    dat <- rbind(dat, workingregion) #, fill=TRUE
}
兹科尔曼

我注意到的第一件事是: dat <- rbind(dat, workingregion)

这行代码在循环中动态增长一个向量,这是不建议的并且会很慢。

我知道这不能回答你关于并行化这个循环的问题。然而,我只是通过一个类似的练习来收集 100,000 个 SQL 查询的结果,并通过内存意识将我的代码加速了 60 倍。

我还将我的代码与foreach%dopar%并行这是 Windows 的理想选择,并且很容易建立一个集群(每个核心上的 R 实例)。

下面是一个有帮助的例子:

library(parallel)
library(doParallel)
library(snow)

# Uses all but one core
cl = makeCluster(detectCores() - 1)

# Necessary to give your instances of R on each core the necessary tools to do what 
# happens in loop 
clusterExport(cl, '<variable names>')
clusterEvalQ(cl, library(packages ))

# parallel loop for going through each region (in your case)
foreach(currentregion = splitdt) %dopar% # iterates over splitdt to cores
{
<body of loop>
}

# Shut down cluster
stopCluster(cl)
stopImplicitCluster()

以下是一些有关加速 R 代码的资源:http : //adv-r.had.co.nz/Performance.html(由该人自己编写https://csgillespie.github.io/efficientR/performance.html

希望这会有所帮助,祝你好运!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在R中加速此简单功能

来自分类Dev

打印格式化的R代码

来自分类Dev

此R代码中的%>%或%。%运算符

来自分类Dev

如何在R函数中重用代码?

来自分类Dev

此代码O(R * C)的最坏情况如何?

来自分类Dev

依赖代码并行化

来自分类Dev

如何使用asyncio并行化阻止代码

来自分类Dev

如何最小化此ajax代码?

来自分类Dev

我如何使此R代码运行得更快

来自分类Dev

打印格式化的R代码

来自分类Dev

R代码运行太慢,如何重写此代码

来自分类Dev

我如何加快我的R代码?

来自分类Dev

如何使用openmp并行化代码以减少矩阵加和

来自分类Dev

加速此Python代码以进行大输入

来自分类Dev

如何从C代码发出R异常信号

来自分类Dev

如何加快R代码

来自分类Dev

R代码错误-如何使用Magrittr重写代码

来自分类Dev

如何在R中向量化此代码

来自分类Dev

MPI帮助如何并行化我的代码

来自分类Dev

如何在R中获取代码?

来自分类Dev

如何估计在R中使用并行计算运行ML代码的时间?

来自分类Dev

如何最小化此PHP代码

来自分类Dev

如何使此R代码更优雅?

来自分类Dev

此R代码如何工作?

来自分类Dev

如何完成以下R代码

来自分类Dev

如何加速我的代码?

来自分类Dev

如何简化此 R 代码以 rbind 列表的所有表?

来自分类Dev

如何缩短此代码或此真实代码?

来自分类Dev

如何更新R包中的函数代码?