From: Ales Ziberna <aleszib_at_gmail.com>

Date: Fri 16 Dec 2005 - 20:36:34 EST

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 Dec 16 20:46:30 2005

Date: Fri 16 Dec 2005 - 20:36:34 EST

- Original Message ----- From: "Ales Ziberna" <aleszib@gmail.com> To: "R-help" <r-help@stat.math.ethz.ch> Sent: Wednesday, December 14, 2005 6:05 PM Subject: The fastest way to select and execute a few selected functions inside a function

Dear useRs?

For examples, if I have function "myf" and the possible functions that I want to call are "mean", "max" and "sum". I have thought of one way (myf) to do that and am interested if there maybe exists a faster way (the speed is very important, since this can be repeated millions of times in my function).

myf<-function(FUN, x){

f<-list(mean=mean, max=max, sum=sum)

res<- vector( mode="list")

for(i in FUN){

res[[i]]<-f[[i]](x) } return(res)

}

myf(FUN=c("mean","max"),x=1:10)

In this case, it would be faster if I would compute all functions, even if I need only one:

myf.all<-function(x){

list(mean=mean(x), max=max(x), sum=sum(x))

}

> gc();system.time(for(i in 1:10000)myf.all(1:20))

used (Mb) gc trigger (Mb) max used (Mb)

Ncells 165659 4.5 350000 9.4 350000 9.4

Vcells 61135 0.5 786432 6.0 283043 2.2

[1] 0.90 0.00 1.08 NA NA

> gc();system.time(for(i in 1:10000)myf(FUN="mean",1:20))

used (Mb) gc trigger (Mb) max used (Mb)

Ncells 165659 4.5 350000 9.4 350000 9.4

Vcells 61135 0.5 786432 6.0 283043 2.2

[1] 1.14 0.00 1.40 NA NA

This does (usually) not happen in my case, since most of functions I consider are more complex.

Thanks in advance for any suggestions!

Best regards,

Ales Ziberna

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 Dec 16 20:46:30 2005

*
This archive was generated by hypermail 2.1.8
: Sat 17 Dec 2005 - 02:30:54 EST
*