[R] timing a function

Alexander Shenkin ashenkin at ufl.edu
Mon May 17 23:32:22 CEST 2010


You could also put the call to system.time inside the function itself:

f = function(x) {
    system.time({
        ... #function's code
        ret_val = ...
    }); flush.console();
    return ret_val;
}

i s'pose you'd miss out on the time taken to jump to the function code,
return the value, etc, but for functions that are heavy at all, that
wouldn't trip you up.

allie

On 5/17/2010 2:06 PM, Barry Rowlingson wrote:
> On Mon, May 17, 2010 at 6:24 PM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
>
>   
>> Try
>>  system.time(y <- f(x))
>>
>> and see ?"=".
>>
>>  -Peter Ehlers
>>     
>  Ah ha. That explains the curly brackets I saw in a posting with
> system.time on stack overflow just now:
>
>  system.time({y=f(x)})
>
>  works as expected since the {} pair make a new code block. Also you
> can then time more than one statement:
>
>  system.time({y=f(x);z=g(y)})
>
>  - gives the total time for f(x) and g(y).
>
> Barry
>
> ______________________________________________
> R-help at r-project.org 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.
>



More information about the R-help mailing list