Skip to content

generating a sequence

4 messages · Brian Ripley, Martin Maechler, Jens Oehlschlägel-Akiyoshi +1 more

#
OK, so we have now seen essentially three solutions:

Me:

A <- matrix(1, n-1, n-1)
rA <- row(A)
rA[rA + col(A) <= n]

Jonathan Rougier:

rA <- outer(1:n, 1:n, "-")
rA[rA>=1]

Andy Royle:

sequence((n-1):1)

Timings for n=500 on a 1994 Sun Sparc 20:

3.29, 4.68 and 23.86 seconds.

I deliberately did not use outer, as it calls apply and 
is normally quite slow.  row and col are internal operations and fast.
#
BDR> OK, so we have now seen essentially three solutions:
    BDR> Me:

    BDR> A <- matrix(1, n-1, n-1)
    BDR> rA <- row(A)
    BDR> rA[rA + col(A) <= n]

    BDR> Jonathan Rougier:

    BDR> rA <- outer(1:n, 1:n, "-")
    BDR> rA[rA>=1]

    BDR> Andy Royle:

    BDR> sequence((n-1):1)

    BDR> Timings for n=500 on a 1994 Sun Sparc 20:

    BDR> 3.29, 4.68 and 23.86 seconds.

    BDR> I deliberately did not use outer, as it calls apply and 
    BDR> is normally quite slow.  row and col are internal operations and fast.

Thanks a lot.

I hope this reminds more people than just me
that we (R users) "desperately" need someone who

rewrites  apply(),lapply(), and tapply()  
to become much faster (as they are with S-plus [34].y, but not 5.[12])...

Anyone working on this ?

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
As this looks like a funny performance competition, 
here a very tricky algorithm I once received from Angelo Canty

vecseq <- function(x,y=NULL) {
    # x1:y1,x2:y2 ... Angelo Canty 8/97
    # parameter handling (c) JOA 1997
        if (missing(y)) {y <- x; x <- 1}
        if (length(y)==1) y <- rep(y,length(x))
        if (length(x)==1) x <- rep(x,length(y))
        str <- paste("c(",paste(x,y,sep=":",collapse=","),")")
        eval(parse(text=str))
}
+ vecseq((n-1):1)
+ })
[1] 0.04 0.00 0.04   NA   NA
+ A <- matrix(1, n-1, n-1)
+ rA <- row(A)
+ rA[rA + col(A) <= n]
+ })
[1] 0.33 0.00 0.33   NA   NA
+ rA <- outer(1:n, 1:n, "-")
+ rA[rA>=1]
+ })
[1] 0.62 0.00 0.62   NA   NA
+ sequence((n-1):1)
+ })
[1] 2.18 0.01 2.27   NA   NA

So, vecsec() is more flexible and much faster than R's sequence,
why not redefine sequence ?
(timings on PII 400 NT4.0)

Regards


Jens Oehlschlaegel-Akiyoshi

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
"Jens Oehlschl?gel-Akiyoshi" <jens.oehlschlaegel-akiyoshi at mdfactory.de> writes:
....
Hmm. vecseq is certainly impressively fast, but I tend to find any
routine that involves parsing suspicious.

sequence<-function(nvec)unlist(lapply(nvec,function(x)1:x))

clocks in at 0.09 (if you gc() first) and would seem to be equivalent
to the current one. Still two times slower than vecseq, though...