timing a function

classic Classic list List threaded Threaded
10 messages Options
pdb
Reply | Threaded
Open this post in threaded view
|

timing a function

pdb
Hi,
I want to time how long a function takes to execute. Any clues on what to search for to achieve this?

Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: timing a function

Mohamed Lajnef

Hi,

? proc.time() for more help

regards
Ml

pdb <[hidden email]> a écrit :

>
> Hi,
> I want to time how long a function takes to execute. Any clues on what to
> search for to achieve this?
>
> Thanks in advance.
> --
> View this message in context:  
> http://r.789695.n4.nabble.com/timing-a-function-tp2126319p2126319.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: timing a function

Joris FA Meys
In reply to this post by pdb
?system.time can help too.

On Tue, May 4, 2010 at 11:07 PM, pdb <[hidden email]> wrote:

>
> Hi,
> I want to time how long a function takes to execute. Any clues on what to
> search for to achieve this?
>
> Thanks in advance.
> --
> View this message in context:
> http://r.789695.n4.nabble.com/timing-a-function-tp2126319p2126319.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>



--
Joris Meys
Statistical Consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

Coupure Links 653
B-9000 Gent

tel : +32 9 264 59 87
[hidden email]
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

        [[alternative HTML version deleted]]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: timing a function

Gustave Lefou
Dear all,

I have a function f(x).
> ptm <- proc.time()
> y=f(x)
> proc.time() - ptm

works fine for me.

However it is not possible to write

> system.time(y=f(x))

and

> system.time(f(x))

does not store the value of f(x).

Should I prefer the 3 lines with proc.time ?

Thank you very much,
Gustave



2010/5/4 Joris Meys <[hidden email]>

> ?system.time can help too.
>
> On Tue, May 4, 2010 at 11:07 PM, pdb <[hidden email]> wrote:
>
> >
> > Hi,
> > I want to time how long a function takes to execute. Any clues on what to
> > search for to achieve this?
> >
> > Thanks in advance.
> > --
> > View this message in context:
> > http://r.789695.n4.nabble.com/timing-a-function-tp2126319p2126319.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [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<http://www.r-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> --
> Joris Meys
> Statistical Consultant
>
> Ghent University
> Faculty of Bioscience Engineering
> Department of Applied mathematics, biometrics and process control
>
> Coupure Links 653
> B-9000 Gent
>
> tel : +32 9 264 59 87
> [hidden email]
> -------------------------------
> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: timing a function

Peter Ehlers
On 2010-05-17 10:49, Gustave Lefou wrote:

> Dear all,
>
> I have a function f(x).
>> ptm<- proc.time()
>> y=f(x)
>> proc.time() - ptm
>
> works fine for me.
>
> However it is not possible to write
>
>> system.time(y=f(x))
>
> and
>
>> system.time(f(x))
>
> does not store the value of f(x).
>
> Should I prefer the 3 lines with proc.time ?

No. You should be careful in your use of '=' which is only allowed at
top level.

Try
  system.time(y <- f(x))

and see ?"=".

  -Peter Ehlers

>
> Thank you very much,
> Gustave
>
>
>
> 2010/5/4 Joris Meys<[hidden email]>
>
>> ?system.time can help too.
>>
>> On Tue, May 4, 2010 at 11:07 PM, pdb<[hidden email]>  wrote:
>>
>>>
>>> Hi,
>>> I want to time how long a function takes to execute. Any clues on what to
>>> search for to achieve this?
>>>
>>> Thanks in advance.
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/timing-a-function-tp2126319p2126319.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> [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<http://www.r-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>> --
>> Joris Meys
>> Statistical Consultant
>>
>> Ghent University
>> Faculty of Bioscience Engineering
>> Department of Applied mathematics, biometrics and process control
>>
>> Coupure Links 653
>> B-9000 Gent
>>
>> tel : +32 9 264 59 87
>> [hidden email]
>> -------------------------------
>> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [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<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: timing a function

barry rowlingson
On Mon, May 17, 2010 at 6:24 PM, Peter Ehlers <[hidden email]> 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

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: timing a function

Gustave Lefou
Thank you Bill, Peter and Barry.



2010/5/17 Barry Rowlingson <[hidden email]>

> On Mon, May 17, 2010 at 6:24 PM, Peter Ehlers <[hidden email]> 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
>

        [[alternative HTML version deleted]]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: timing a function

Alexander Shenkin
In reply to this post by barry rowlingson
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 <[hidden email]> 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
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: timing a function

Gustave Lefou
Thank you Alexander

2010/5/17 Alexander Shenkin <[hidden email]>

> 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 <[hidden email]>
> 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
> >
>  > ______________________________________________
> > [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<http://www.r-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> ______________________________________________
> [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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: timing a function

Gustave Lefou
In reply to this post by Alexander Shenkin
Dear all,

Just one last question. There seems to be no problem in writing

> z = system.time(y <- f(x))

or

> z <- system.time(y <- f(x))

Then z contains the named vector of the elapsed times, and y the value of
the function f(x).

Am I right ?

Thank you very much,
Gustave

2010/5/17 Alexander Shenkin <[hidden email]>

> 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 <[hidden email]>
> 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
> >
>  > ______________________________________________
> > [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<http://www.r-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> ______________________________________________
> [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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.