From ggrothendieck at gmail.com  Sat Nov 18 17:57:04 2006
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Sat, 18 Nov 2006 11:57:04 -0500
Subject: [Rd] overhead of function calls
In-Reply-To: <20061118165039.GA18991@pu100877.student.princeton.edu>
References: <20061118165039.GA18991@pu100877.student.princeton.edu>
Message-ID: <971536df0611180857p472f6de9r60ecb6cf209697c7@mail.gmail.com>

I don't know if this would have an appreciable effect or not but
you could also check whether passing the free variables explicitly
speeds it up so that they don't have to be looked up each time
in the outside environment.

On 11/18/06, Tamas K Papp <tpapp at princeton.edu> wrote:
> Hi,
>
> Profiling shows that 65-70% of the time of my program is spent inside
> a single function -- this is not surprising, as it is inside an
> optimize call inside a loop (this is a dynamic programming problem).
> I would like to speed this up.
>
> The function does very little: has a single argument, evaluates a
> spline at that argument, does some simple arithmetic with it (adding
> constants, multiplication).  With R being a functional programming
> language, I implemented this by calling several functions inside the
> function:
>
>          ## RHS of bellman equation
>          f <- function(knext,k,ei) {
>            util(consf(knext,k))+quickeval(knext,gridsecpp,Vkbarpp)
>          }
>
> where quickeval evaluates a spline at knext (on gridsecpp, pp-form
> Vkbarpp), util is a function in the environment, and so is consf:
>
>          ## consumption
>           consf <- function(knext,k) {
>            rp*k+W+knext*A
>          }
>
> A, W, and rp are constants in the environment.
>
> Then I call
>
> optimize(f, lower=...,upper=...,k=...)
>
> to find the maximum.
>
> Questions:
>
> 1. does function calling give a significant overhead in R?  If so, I
> would rewrite the function into a single one.  I tried to test this by
>
> > f <- function(x) 1+x
> > g <- function(x) f(x)
> > x <- rnorm(1e6)
> > system.time(sapply(x,f))
> [1] 11.315  0.157 11.735  0.000  0.000
> > system.time(sapply(x,g))
> [1] 8.850 0.140 9.283 0.000 0.000
> > system.time(for (i in seq_along(x)) f(x[i]))
> [1] 2.466 0.036 2.884 0.000 0.000
> > system.time(for (i in seq_along(x)) g(x[i]))
> [1] 3.548 0.045 4.165 0.000 0.000
>
> but I find that hard to interpret -- the overhead looks significant in
> the first case, but something strange (at least to my limited
> knowledge) is happening with sapply.
>
> 2. Do calls to .C or .Fortran carry large overhead?  If they don't, I
> would recode f in either.
>
> Thanks,
>
> Tamas
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] overhead of function calls
Gabor Grothendieck