Speeed! [Two-Stage / GS Designs]
❝ ❝ for(j in seq_along(run)) v1 <- c(v1, rnorm(1, mean=0, sd=1))
❝
❝ This is a funny way to do it. There will be constant memory re-allocation of the vector this way. I guess that's what takes the time.
Exactly. That’s really stupid (see the R Inferno Circle 2). Detlew cured me.
❝ The idea is (and pardon me for going from a for loop to a while loop; this is just because I don't know of a way to make a for loop increase by more than one per iteration, but I am sure you can do that):
I have learned that one never ever should try to manipulate the counter of a
for
-loop. 
runs <- 1e7
ptm <- proc.time()
i <- 1
while (i <= runs) {
# Do.Something(i)
i <- i+1
}
t1 <- proc.time()-ptm
ptm <- proc.time()
i <- 1
while (i <= runs) {
# Do.Something(i)
# Do.Something(i+1)
i <- i+2
}
t2 <- proc.time()-ptm
cat("\n",
"simple :", t1[3], "seconds\n",
"partial:", t2[3], "seconds\n")
simple : 5.07 seconds
partial: 2.55 seconds
But: Quite often the number of iterations is not known beforehand (here it must be even) and the time-consuming part is the
Do.Something()
.Reminds me on my first Pascal-lessons almost 40 years ago.
Instead of better use
y := 2*x; y := x+x; (addition faster than multiplication)
y := x/2; y := 0.5*x; (multiplication faster than division)
Dif-tor heh smusma 🖖🏼 Довге життя Україна!
![[image]](https://static.bebac.at/pics/Blue_and_yellow_ribbon_UA.png)
Helmut Schütz
![[image]](https://static.bebac.at/img/CC by.png)
The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes
Complete thread:
- Gimmick & survey Helmut 2015-02-20 01:47
- Gimmick & survey nobody 2015-02-20 08:25
- Gimmick & survey Helmut 2015-02-20 13:01
- Gimmick & survey nobody 2015-02-20 14:18
- Speeed! Helmut 2015-02-20 15:38
- Speeed! nobody 2015-02-20 16:37
- Speeed! ElMaestro 2015-02-20 23:48
- Speeed!Helmut 2015-02-21 01:11
- Unrolled example ElMaestro 2015-02-21 01:32
- Unrolled example Helmut 2015-02-21 01:50
- Unrolled example ElMaestro 2015-02-21 02:27
- Increasing vector & another example Helmut 2015-02-21 13:13
- Unrolled example ElMaestro 2015-02-21 02:27
- Unrolled example Helmut 2015-02-21 01:50
- Speeed! nobody 2015-02-21 21:02
- Unrolled example ElMaestro 2015-02-21 01:32
- Speeed!Helmut 2015-02-21 01:11
- Speeed! Helmut 2015-02-20 15:38
- Gimmick & survey nobody 2015-02-20 14:18
- Gimmick & survey Helmut 2015-02-20 13:01
- Gimmick & survey nobody 2015-02-20 08:25