Re: [R] eval(parse(text vs. get when accessing a function

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 06 Jan 2007 - 14:03:56 GMT

Here are a few more timing alternatives from slowest to fastest. Of the ones that dynamically construct the name of the frunction (all except last example), get seems to be the fastest although its still 6x slower than the if statement.

> f.1 <- function(x) x+1
> f.2 <- function(x) x+2
>
> n <- 10^4; i <- 1; x <- 10
>
> system.time(
+ replicate(n, eval(parse(text=paste('f.', i, sep='')))(x)) + )
[1] 1.27 0.00 1.31 NA NA
>
> system.time(

+ replicate(n, match.fun(paste("f.", i, sep=''))(x) ) + )
[1] 1.15 0.01 1.20 NA NA
>
> system.time(

+ replicate(n, do.call(paste("f.", i, sep=''), list(x)) ) + )
[1] 0.99 0.00 1.02 NA NA
>
> system.time(

+ replicate(n, get(paste("f.", i, sep=''))(x) ) + )
[1] 0.93 0.00 0.94 NA NA
>
> system.time(

+ replicate(n, {if(i %% 2 == 0) f.1 else f.2}(x) ) + )
[1] 0.15 0.00 0.15 NA NA

On 1/5/07, jim holtman <jholtman@gmail.com> wrote:
> The other reason for considering which of the different approaches to use
> would be performance:
>
> > f.1 <- function(x) x+1
> > f.2 <- function(x) x+2
> >
> > system.time({
> + for (i in 1:100000){
> + eval(parse(text=paste('f.', i%%2+1, sep='')))(i)
> + }
> + })
> [1] 6.96 0.00 8.32 NA NA
> >
> > system.time({
> + for (i in 1:100000){
> + {if(i %% 2 == 0) f.1 else f.2}(i)
> + }
> + })
> [1] 0.52 0.00 0.61 NA NA
> >
> >
>
> eval(parse...) seems to be an order of magnitude slower. It would make a
> difference if you were calling it several thousand times; so it depends on
> your application.
>
>
> On 1/5/07, Ramon Diaz-Uriarte <rdiaz@cnio.es> wrote:
> >
> > On Friday 05 January 2007 19:35, Bert Gunter wrote:
> > > ??
> > >
> > > Or to add to what Peter Dalgaard said... (perhaps for the case of many
> > more
> > > functions)
> > >
> > > Why eval(parse())? What's wrong with if then?
> > >
> > > g <- function(fpost,x){if(fpost==1)f.1 else f.2 }(x)
> > >
> > > or switch() if you have more than 2 possible arguments? I think your
> > > remarks reinforce the wisdom of Thomas's "axiom" .
> >
> > Thanks, Bert, but as with Peter's solution, your solution forces me to
> > build g
> > ahead of time. And again, I am not sure I see why the attempt to avoid
> > eval(parse(text.
> >
> > Best,
> >
> > R.
> >
> >
> > >
> > > Bert Gunter
> > > Genentech Nonclinical Statistics
> > > South San Francisco, CA 94404
> > >
> > >
> > > -----Original Message-----
> > > From: r-help-bounces@stat.math.ethz.ch
> > > [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Ramon
> > Diaz-Uriarte
> > > Sent: Friday, January 05, 2007 10:02 AM
> > > To: r-help; rdiaz02@gmail.com
> > > Subject: [R] eval(parse(text vs. get when accessing a function
> > >
> > > Dear All,
> > >
> > > I've read Thomas Lumley's fortune "If the answer is parse() you should
> > > usually
> > > rethink the question.". But I am not sure it that also applies (and why)
> > to
> > > other situations (Lumley's comment
> > > http://tolstoy.newcastle.edu.au/R/help/05/02/12204.html
> > > was in reply to accessing a list).
> > >
> > > Suppose I have similarly called functions, except for a postfix. E.g.
> > >
> > > f.1 <- function(x) {x + 1}
> > > f.2 <- function(x) {x + 2}
> > >
> > > And sometimes I want to call f.1 and some other times f.2 inside another
> > > function. I can either do:
> > >
> > > g <- function(x, fpost) {
> > > calledf <- eval(parse(text = paste("f.", fpost, sep = "")))
> > > calledf(x)
> > > ## do more stuff
> > > }
> > >
> > >
> > > Or:
> > >
> > > h <- function(x, fpost) {
> > > calledf <- get(paste("f.", fpost, sep = ""))
> > > calledf(x)
> > > ## do more stuff
> > > }
> > >
> > >
> > > Two questions:
> > > 1) Why is the second better?
> > >
> > > 2) By changing g or h I could use "do.call" instead; why would that be
> > > better?
> > > Because I can handle differences in argument lists?
> > >
> > >
> > >
> > > Thanks,
> > >
> > >
> > > R.
> >
> > --
> > Ramón Díaz-Uriarte
> > Centro Nacional de Investigaciones Oncológicas (CNIO)
> > (Spanish National Cancer Center)
> > Melchor Fernández Almagro, 3
> > 28029 Madrid (Spain)
> > Fax: +-34-91-224-6972
> > Phone: +-34-91-224-6900
> >
> > http://ligarto.org/rdiaz
> > PGP KeyID: 0xE89B3462
> > (http://ligarto.org/rdiaz/0xE89B3462.asc)
> >
> >
> >
> > **NOTA DE CONFIDENCIALIDAD** Este correo electrónico, y en s...{{dropped}}
> >
> > ______________________________________________
> > 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
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>
> [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>
>
>

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 and provide commented, minimal, self-contained, reproducible code. Received on Sun Jan 07 01:20:44 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 06 Jan 2007 - 15:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.