Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中的for循环中嵌套循环时加速_R - Fatal编程技术网

在R中的for循环中嵌套循环时加速

在R中的for循环中嵌套循环时加速,r,R,在R中,我有以下示例模块,该模块重复for循环n次: function(n){ #inputs - n - number of results required #reserve n spaces for results r_num_successes <- 1:n #start looping n times for(i in 1:n){ #set first uniform "random" deviate equal to 0.05

在R中,我有以下示例模块,该模块重复for循环
n
次:

function(n){
#inputs - n - number of results required
    #reserve n spaces for results
    r_num_successes <- 1:n

    #start looping n times
    for(i in 1:n){

        #set first uniform "random" deviate equal to 0.05 and number of successes to 0
        current_unif <- 0.05
        num_successes <- 0

        #start while loop that updates current_unif - it runs as long as 
        #current_unif is less than 0.95, increments num_successes each loop
        while(current_unif < 0.95){

            #set current_unif to a uniform random deviate between the
            #existing current_unif and 1
            current_unif <- runif(1,current_unif)
            num_successes <- num_successes + 1
        }

        #set the i-th element of the results vector to that final num_successes
        #generated by the while loop
        r_num_successes[i] <- num_successes
    }

            #output the mean of all the successes
    return(mean(r_num_successes))
}
函数(n){
#输入-n-所需结果的数量
#为结果保留n个空格

r_num_successfuls使用纯r无法显著提高速度。字节编译会给您带来一点改进,但您需要转向编译代码以获得任何显著的速度提升

更新:这里有一个Rcpp解决方案,只针对德克:)

>nCode
>库(内联)
>nFunRcpp库(编译器)
>nFunCmp系统时间(nFun(1e5))
用户系统运行时间
3.100   0.000   3.098 
>系统时间(nFunCmp(1e5))
用户系统运行时间
2.120   0.000   2.114 
>系统时间(nFunRcpp(1e5))
用户系统运行时间
0.010   0.000   0.016 

使用纯R无法显著提高此操作的速度。字节编译将给您带来一点改进,但您需要转向编译代码以获得任何显著的速度提升

更新:这里有一个Rcpp解决方案,只针对德克:)

>nCode
>库(内联)
>nFunRcpp库(编译器)
>nFunCmp系统时间(nFun(1e5))
用户系统运行时间
3.100   0.000   3.098 
>系统时间(nFunCmp(1e5))
用户系统运行时间
2.120   0.000   2.114 
>系统时间(nFunRcpp(1e5))
用户系统运行时间
0.010   0.000   0.016 

为了完整起见,我建议@JoshuaUlrich:

R> res <- benchmark(nFun(1e5L), nFunCmp(1e5L), nFunRcpp(1e5L), nFun2Rcpp(1e5L),
+                  columns = c("test", "replications", "elapsed", "relative"),
+                  replications=10,
+                  order="relative")
R> print(res)
               test replications elapsed  relative
4 nFun2Rcpp(100000)           10   0.117   1.00000
3  nFunRcpp(100000)           10   0.122   1.04274
2   nFunCmp(100000)           10  13.845 118.33333
1      nFun(100000)           10  23.212 198.39316
R> 
并将作业更改为

rns[i] = num_successes;
而不是使用
。push_back()
,这会使内存分配更高效


编辑结果是不准确的,是随机算法的反映。如果我把一组“代码> > SET(种子)(/CODE)添加到每一个,两个C++版本之间的时间是相同的。这里没有可测量的增益。

只是为了完整性,这里是我对JoshuaUlrich提出的建议:

R> res <- benchmark(nFun(1e5L), nFunCmp(1e5L), nFunRcpp(1e5L), nFun2Rcpp(1e5L),
+                  columns = c("test", "replications", "elapsed", "relative"),
+                  replications=10,
+                  order="relative")
R> print(res)
               test replications elapsed  relative
4 nFun2Rcpp(100000)           10   0.117   1.00000
3  nFunRcpp(100000)           10   0.122   1.04274
2   nFunCmp(100000)           10  13.845 118.33333
1      nFun(100000)           10  23.212 198.39316
R> 
并将作业更改为

rns[i] = num_successes;
而不是使用
。push_back()
,这会使内存分配更高效


编辑结果不准确,反映了随机算法< /代码>每个版本,两个C++版本之间的时间是相同的。这里没有可测量的增益。请用文字描述这个函数应该做什么。看起来你只是在使用“代码> N<代码>,从一个5%的成功率的均匀样本中抽取。”安德烈显然在试图提出一种测量“COD”分布的方法。e> runif
这值得提交给DailyWTF:-)@CarlWitthoft Ahah。我当时认为这是最混乱的随机采样器比赛的新项目…@dplanet我看到你在代码中添加了注释。这只是在毫无意义的代码中添加注释。请用文字描述你正在尝试做什么。@CarlWitthoft-我懒散了,所以+1。然而,这只是我正在制作的一个函数的一个简单示例,它有很多行,因此需要优化。我对脚本进行了评论-我发现很难解释,因为除了证明我的观点之外,它没有任何用处。请用文字描述这个函数应该做什么。看起来你根本不是aking
n
以5%的成功率从一个统一的样本中抽取。@Andrie显然在试图想出一种测量
runif
分布的方法,值得提交给DailyWTF:-)@CarlWitthoft-Ahah。我当时认为这是一个最混乱的随机抽样比赛的新项目…@dplanet我看到你在代码中添加了注释。这只是在毫无意义的代码中添加了注释。请用文字描述你正在尝试做什么。@CarlWitthoft-我懒散了so+1。然而,这只是我正在制作的一个简单的函数示例,它有很多行,因此需要优化。我对脚本进行了注释-我发现很难扩展因为它除了证明我的观点之外没有任何用处。天哪,Josh发布了一个Rcpp解决方案?太棒了。我只有一个温和的建议:使用
rbenchmark::benchmark()
用于计时。哦,可能会预先分配向量。但是STL在计算平均值时的使用非常好。给我留下深刻印象:)@DirkEddelbuettel:谢谢。我的“STL在计算平均值时的使用非常好”搜索结果是否如此。;)我不认为有理由使用
rbenchmark
,因为在这种情况下没有竞争。我还从你的博客中刷了一点代码来开始。:)天哪,Josh发布了一个Rcpp解决方案?太棒了。我只有一个温和的建议:使用
rbenchmark::benchmark()
用于计时。哦,可能会预先分配向量。但是STL在计算平均值时的使用非常好。给我留下深刻印象:)@DirkEddelbuettel:谢谢。我的“STL在计算平均值时的使用非常好”搜索的结果是这样的。;)我不认为有理由使用
rbenchmark
,因为在这种情况下没有竞争。我还从你的博客中刷了一些代码来开始。:)你是对的,使用rbenchmark的比率好多了。:)你是对的,使用rbenchmark的比率好多了。:)