Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

[R] sequence()

Robin Hankin

2005-07-22


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

______________________________________________
R-help@(protected)
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
©2008 r-help.com - Jax Systems, LLC, U.S.A.