R-alpha: loop speed

Thomas Lumley thomas@biostat.washington.edu
Mon, 10 Nov 1997 12:19:01 -0800 (PST)


On Mon, 10 Nov 1997, Paul Gilbert wrote:

> I noticed Doug only circulated the following on S-news, but it may be of
> interest to R users who don't follow S-news. His findings are certainly
> consistent with my own, where my problems often force me into the
> element by element type of situation.

It is also interesting to look at the allegedly realistic if extreme
benchmark program provided with Matt Calder's partial S compiler. 

        LMS <- function(M, N)
        {
                ### Pre-allocate result and filter. ###
                R <- matrix(0,nrow=M, ncol=5)
                W <- rep(0,5)
                for (i in 1:M) {
                        ### Simulate MA(1) ###
                        Z <- rnorm(N+1)
                        X <- Z[2:(N+1)] + 0.5 * Z[1:N]
                        ### Perform LMS ###
                        for (j in 5:N) {
                                U <- X[j:(j-4)]
                                E <- Z[j+1] - sum(W * U)
                                W <- W + 0.1* E * U
                        }
                        ### Save final result ###
                        R[i,] <- W
                }
                return(R)
        }


I used this in S, R and compiled to C on an Ultra 1 (Solaris) and in R and
compiled to C on a Pentium Pro 150 under Linux

The results are quite interesting. These are CPU times in seconds for a
fixed value of N (100, I believe, but it was some time ago)

	Ultra             Intel
M     R     C    S      R      C
100  7.24  2.08  11    6.38   0.79
200 14.19  4.06  25.8  12.36  1.56
300 21.59  6.3   56.8  18.89  2.31
400 28.76  8.37  89    24.97  3.09
800 57.62 16.42 295    50.33  6.16

The time taken by R scales roughly linearly with M, S time grows much
faster. The two are comparable for small M. It's also interesting that the
Intel machine was slightly faster, though this doesn't translate to disk
and memory intensive tasks. 


Thomas Lumley
------------------------------------------------------+------
Biostatistics		: "Never attribute to malice what  :
Uni of Washington	:  can be adequately explained by  :
Box 357232		:  incompetence" - Hanlon's Razor  :
Seattle WA 98195-7232	:				   :
------------------------------------------------------------


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=