[R] sequence()

From: Robin Hankin <r.hankin_at_noc.soton.ac.uk>
Date: Fri 22 Jul 2005 - 18:36:00 EST


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@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri Jul 22 18:42:16 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:55 EST