在相同计算(R)的每次迭代中增加计算时间(Increasing computational time at every iteration of the same calculations (R))

编程入门 行业动态 更新时间:2024-10-26 18:20:25
在相同计算(R)的每次迭代中增加计算时间(Increasing computational time at every iteration of the same calculations (R))

我在一个循环中运行1000个模拟。 所有模拟都应该采用相似的计算时间,因为所有程序都相同,但我的计算时间是{6.496,7.680,9.464,10.976,...,141.460,145.276,143.148}。 它们随着时间的推移而急剧增加。

我的猜测是与时间记忆中没有空间或类似的东西有关,但我对计算机科学知之甚少。 我认为我需要在循环中添加一个额外的步骤,在那里我删除使用内存的垃圾(如重置而不删除以前的计算),这应该解决这个不必要的浪费时间的问题。

我很欣赏这个解决方案,但也解释了为什么在你没有R的解决方案时会发生这种情况。

我正在使用的代码是

ptm <- proc.time() init_pars = c(0.8,0.0175,0.1) pars=init_pars n_it = 50 M = matrix(nrow=n_it,ncol=3) for (i in 1:n_it){ print(c(pars[1],pars[2],pars[3])) n_it = 10 S=list() for (j in 1:n_it){ rec_tree = reconst_tree(bt=s2$t,pars=pars,tt=15) S[[j]] = rec_tree } pars = mle_dd_setoftrees(S) pars = c(pars$lambda,pars$beta,pars$mu) M[i,]=c(pars[1],pars[2],pars[3]) print(proc.time() - ptm) ptm <- proc.time() }

函数reconst_tree创建独立模拟,mle_dd_setoftrees根据一组模拟计算估计值,然后将估计值存储在矩阵M中。

I am running 1000 simulations in a loop. All simulations should take similar computational time because are all the same procedure, however my computational times are { 6.496, 7.680, 9.464 , 10.976, ..., 141.460, 145.276, 143.148}. They are badly increasing with time.

My guess is that has something to do with no space in the temporal memory or something like that, but I know very little about computer sciences. I would think that I need to just add an extra step whithin the loop where I delete the garbage that is using the memory (like a reset without deleting the previous calculations) and that should solve the problem with this unnecessary waste of time.

I appreciate a solution of this, but also a small explanation of why this happens in case you do not have a solution for R.

The code I am using is

ptm <- proc.time() init_pars = c(0.8,0.0175,0.1) pars=init_pars n_it = 50 M = matrix(nrow=n_it,ncol=3) for (i in 1:n_it){ print(c(pars[1],pars[2],pars[3])) n_it = 10 S=list() for (j in 1:n_it){ rec_tree = reconst_tree(bt=s2$t,pars=pars,tt=15) S[[j]] = rec_tree } pars = mle_dd_setoftrees(S) pars = c(pars$lambda,pars$beta,pars$mu) M[i,]=c(pars[1],pars[2],pars[3]) print(proc.time() - ptm) ptm <- proc.time() }

the function reconst_tree create independent simulations and mle_dd_setoftrees caluculate estimations from a set of simulations, then I store the estimations in the matrix M.

最满意答案

代码的违规部分是这样的:

S=list() for (j in 1:n_it){ rec_tree = reconst_tree(bt=s2$t,pars=pars,tt=15) S[[j]] = rec_tree }

你在这做什么被称为“成长对象”

R的灵活性之一就是花费大量时间来决定分配给对象的内存量。 每次向列表中添加元素时,R都会重新评估每个元素的内容,从而导致循环及时爬行。

为循环构造良好可以通过在循环之前分配适当的容器来避免这种情况。

The offending part of your code is this:

S=list() for (j in 1:n_it){ rec_tree = reconst_tree(bt=s2$t,pars=pars,tt=15) S[[j]] = rec_tree }

What you are doing here is termed "growing an object"

One of the trade off for R's flexibility is that it spends a lot of time deciding how much memory to allocate to objects. Each time you add an element to your list, R is reevaluating the contents of each element, causing your loop to come to a horrific crawl in time.

A well constructed for loop can avoid this by allocating an appropriate container ahead of the loop.

更多推荐

本文发布于:2023-08-01 18:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:迭代   时间   Increasing   computational   iteration

发布评论

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

>www.elefans.com

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