[18615] in s-news-athena
[S] Is always better vectorize?
daemon@ATHENA.MIT.EDU (Eusebio Arenal)
Tue Jul 13 07:16:02 1999
Date: Tue, 13 Jul 1999 13:12:31 +0200 (METDST)
Message-Id: <199907131112.NAA13057@iscar.cpd.uva.es>
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
To: S-news@wubios.wustl.edu
From: slist@eio.uva.es (Eusebio Arenal)
Is there any general rule about when I must vectorize an expresion or when I
can use a loop? It seems that the relative speed of for - apply depends on
the size of the loop. See the example:
> zzz <- matrix(runif(100), ncol = 10)
> dos.time(for(i in 1:ncol(zzz))
mean(zzz[, i]))
[1] 0
> dos.time(apply(zzz, 2, mean))
[1] 0.00994873
> zzz <- matrix(runif(10000), ncol = 100)
> dos.time(for(i in 1:ncol(zzz))
mean(zzz[, i]))
[1] 0.06994629
> dos.time(apply(zzz, 2, mean))
[1] 0.07000732
> zzz <- matrix(runif(600000), ncol = 6000)
> dos.time(for(i in 1:ncol(zzz))
mean(zzz[, i]))
[1] 5.380005
> dos.time(apply(zzz, 2, mean))
[1] 4.550049
I've run this example many times with similar results.
I'm using S-plus 4.5r2 with Windows 98 in a K6 (128 Ram)
------------------------------------------------------------------------
Eusebio Arenal Gutierrez Tfo: +34 (9)83 423000 ext. 4258
Dpto. Estadistica e I.O. Fax: +34 (9)83 423013
Facultad de Ciencias Email: slist@eio.uva.es
Universidad de Valladolid
Avda. Prado de la Magdalena, s/n.
Valladolid (SPAIN)
------------------------------------------------------------------------
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news