Re: The fastest way to select and execute a few selected func tions inside a function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: The fastest way to select and execute a few selected func tions inside a function

Liaw, Andy
See if the following helps you any:

> flist <- list(min=min, mean=mean, median=median, max=max, n=length)
> x <- rnorm(1000)
> system.time(for (i in 1:1e4) sapply(flist, function(f) f(x)))
[1] 5.90 0.01 6.05   NA   NA
> system.time(for (i in 1:1e4) sapply(flist[c("mean", "median")],
function(f) f(x)))
[1] 5.70 0.00 5.77   NA   NA
> system.time(for (i in 1:1e4) sapply(flist[c("min", "max")], function(f)
f(x)))
[1] 1.75 0.00 1.75   NA   NA

Andy

From: Ales Ziberna

>
> Dear useRs?
>
>
>
> I have the following problem! I have a function that calls
> one or more
> functions, depending on the input parameters. I am searching
> for the fastest
> way to select and execute the selected functions and return
> their results in
> a list. The number of possible functions is 10, however
> usually only 2 are
> selected (although sometimes more, even all).
>
>
>
> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Loading...