Compare
system.time({
v <- vector()
for (i in 1:10^5) v <- c(v,1)
})
with
system.time({
v <- vector(length=10^5)
for (i in 1:10^5) v[i] <- 1
})
If you don't know exactly how long v will be, use a value that's large
enough, then throw away what's extra.
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Tim Alcon
> Sent: Thursday, June 01, 2006 2:04 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] progressive slowdown during script execution?
>
> I'm an R novice, so I hope my question is a valid one. I'm trying
to
> run the following script in the current version of R.
>
> for (i in 1:1640){for (j in (i+1):1641){
> if (i == 1 && j == 2){x <-
> cor(sage[i,],sage[j,],method="spearman"); y
> <- cor(frie[i,],frie[j,],method="spearman")}
> if (i != 1 || j != 2){x <-
> c(x,cor(sage[i,],sage[j,],method="spearman")); y <-
> c(y,cor(frie[i,],frie[j,],method="spearman"))}}}
>
> It basically just finds all pairwise correlations of the rows in a
> matrix for each of two matrices and stores the results for
> each matrix
> in a vector. The problem I seem to be running into is that
> it seems to
> slow way down during execution somehow. When I first tried
> running it I
> stopped execution to see how fast it was running, before trying to
> compute the whole job (the two matrices each have 1641 rows).
> Based on
> what I saw, I figured it would easily finish overnight.
> Instead, it was
> still running almost 24 hours later. To quantify this a
> little better I
> checked it after running for 5 minutes, at which point it had added
> 79120 correlations to each of the x and y vectors. Since
> there should
> be a total of (1641*1640)/2 = 1345620 pairwise correlations in each
> vector when it finishes running, I worked out that it should take
> (1345620/79120)*5 = 85 minutes to run the whole job. However, when I
> checked it after running for 2 hours, it had added only 341870
> correlations to each vector.
>
> Any ideas what I'm doing wrong, or why it would run more slowly the
> longer it runs? Thanks for any help or advice.
>
> Tim
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>