[R] sequence()

Robin Hankin r.hankin at noc.soton.ac.uk
Fri Jul 22 10:36:00 CEST 2005


Hi

Function sequence() repeatedly concatenates
its output, and this is slow.

It is possible to improve on the performance of sequence by
defining

  myseq <- function(x){unlist(sapply(x,function(i){1:i}))}

The following session compares the  performance of
myseq(), and sequence(), at least on my G5:


 > identical(sequence(1:50),myseq(1:50))
[1] TRUE
 > system.time(ignore <- sequence(1:800))
[1] 1.16 0.88 2.07 0.00 0.00
 > system.time(ignore <- sequence(1:800))
[1] 1.14 0.84 1.99 0.00 0.00
 > system.time(ignore <- myseq(1:800))
[1] 0.02 0.02 0.04 0.00 0.00
 > system.time(ignore <- myseq(1:800))
[1] 0.03 0.00 0.03 0.00 0.00
 >

(the time differential is even more marked for longer arguments).

Is there any reason why we couldn't use  this definition instead?



--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743




More information about the R-help mailing list