Finding the time taken by a function

Discussion in 'C Programming' started by srkkreddy@gmail.com, Jan 24, 2005.

  1. Guest

    Hi,

    I have written a large program which makes multiple calls to number of
    functions (also written by me) of the program. Now, I want to know the
    collective time taken by all the calls to a specific function. Is
    there any way I can find this time.
    Thanks in advance,
    Raja Kiran Sandireddy.
     
    , Jan 24, 2005
    #1
    1. Advertising

  2. Yaniv Oliver Guest

    If you don't care how accurate the results are (and don't need better
    precession than just seconds), you can call time() before you call the
    function you want to time, and then after it returns call time() again and
    subtract the values.

    Yaniv

    <> wrote in message
    news:...
    > Hi,
    >
    > I have written a large program which makes multiple calls to number of
    > functions (also written by me) of the program. Now, I want to know the
    > collective time taken by all the calls to a specific function. Is
    > there any way I can find this time.
    > Thanks in advance,
    > Raja Kiran Sandireddy.
    >
     
    Yaniv Oliver, Jan 24, 2005
    #2
    1. Advertising

  3. Yaniv Oliver Guest

    Wow, that was a bad typo, precision.

    "Yaniv Oliver" <yaniv (at) softlink.com> wrote in message
    news:41f4c5b0$1@news.012.net.il...
    > If you don't care how accurate the results are (and don't need better
    > precession than just seconds), you can call time() before you call the
    > function you want to time, and then after it returns call time() again and
    > subtract the values.
    >
    > Yaniv
    >
    > <> wrote in message
    > news:...
    >> Hi,
    >>
    >> I have written a large program which makes multiple calls to number of
    >> functions (also written by me) of the program. Now, I want to know the
    >> collective time taken by all the calls to a specific function. Is
    >> there any way I can find this time.
    >> Thanks in advance,
    >> Raja Kiran Sandireddy.
    >>

    >
    >
     
    Yaniv Oliver, Jan 24, 2005
    #3
  4. Mabden Guest

    Wow, you both top post, which is a Bad Thing.

    Please stop!

    "Yaniv Oliver" <yaniv (at) softlink.com> wrote in message
    news:41f4c643$1@news.012.net.il...
    > Wow, that was a bad typo, precision.
    >
    > "Yaniv Oliver" <yaniv (at) softlink.com> wrote in message
    > news:41f4c5b0$1@news.012.net.il...
    > > If you don't care how accurate the results are (and don't need

    better
    > > precession than just seconds), you can call time() before you call

    the
    > > function you want to time, and then after it returns call time()

    again and
    > > subtract the values.
    > >
    > > Yaniv
    > >
    > > <> wrote in message
    > > news:...
    > >> Hi,
    > >>
    > >> I have written a large program which makes multiple calls to number

    of
    > >> functions (also written by me) of the program. Now, I want to know

    the
    > >> collective time taken by all the calls to a specific function. Is
    > >> there any way I can find this time.
    > >> Thanks in advance,
    > >> Raja Kiran Sandireddy.
    > >>

    > >
    > >

    >
    >
     
    Mabden, Jan 24, 2005
    #4
  5. wrote:
    > Hi,
    >
    > I have written a large program which makes multiple calls to number of
    > functions (also written by me) of the program. Now, I want to know the
    > collective time taken by all the calls to a specific function. Is
    > there any way I can find this time.
    > Thanks in advance,
    > Raja Kiran Sandireddy.
    >


    You are essentially profiling the application. Depending on your
    implementation, you can use an appropriate profiler.

    On most gnu systems, you can use gprof for profiling.
     
    Karthik Kumar, Jan 24, 2005
    #5
  6. On Mon, 24 Jan 2005 11:50:46 +0200, Yaniv Oliver wrote:

    > If you don't care how accurate the results are (and don't need better
    > precession than just seconds), you can call time() before you call the
    > function you want to time, and then after it returns call time() again and
    > subtract the values.


    clock() tends to be better suited for measuring CPU time. However even
    that will have limited resolution so quick functions will probably end up
    as 0 difference most of the time. As mentioned elsewhere a profiler would
    be a good placed to start.

    Lawrence
     
    Lawrence Kirby, Jan 24, 2005
    #6
  7. Subhash Guest

    You can also use gettimeofday function

    Subhash
     
    Subhash, Jan 24, 2005
    #7
  8. On 24 Jan 2005 05:41:19 -0800, in comp.lang.c , "Subhash"
    <> wrote:

    >You can also use gettimeofday function


    Thats not a c function, its a posix one, and may not be available. Besides,
    it would be offtopic here.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Jan 24, 2005
    #8
  9. S.Tobias Guest

    Lawrence Kirby <> wrote:
    > On Mon, 24 Jan 2005 11:50:46 +0200, Yaniv Oliver wrote:


    > > If you don't care how accurate the results are (and don't need better
    > > precession than just seconds), you can call time() before you call the
    > > function you want to time, and then after it returns call time() again and
    > > subtract the values.


    > clock() tends to be better suited for measuring CPU time. However even
    > that will have limited resolution so quick functions will probably end up
    > as 0 difference most of the time.


    [OT]
    If you assume that a computer is a stochastic machine, ie. function calls
    are delayed in random fashion (I think it is a safe bet for ordinary
    desktop computers - it's enough to move your mouse every now and then),
    then statistically you could measure the time a function is executed
    which is below the resolution of the time measurement if you have
    enough samples (in theory at least, but it worked for me in the past
    in another language).

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Jan 24, 2005
    #9
  10. Sanda Guest

    You can use the clock() function to calculate the time. Just call it
    before and after the function calls and substract the two values and
    devide it buy CLK_TCK which is the systems pulse time.

    eg:
    #include<time.h>
    //other include files

    void main()
    {clock_t start, end;

    //codes................

    start=clock();
    //your funcrion calls
    end =clock();
    printf("\n Time taken is %f seconds.\n",(end-start)/CLK_TCK);


    }

    for more information use the help files in your compiler. (I used the
    Turbo c++ compiler for this).
     
    Sanda, Jan 25, 2005
    #10
  11. Al Bowers Guest

    Sanda wrote:
    > You can use the clock() function to calculate the time. Just call it
    > before and after the function calls and substract the two values and
    > devide it buy CLK_TCK which is the systems pulse time.
    >

    CLK_TCK is not defined in Standard C. If you are writing Stanard
    C code you should use the defined macro CLOCKS_PER_SEC which is a
    clock_t type that represents the number per second of the value
    returned by the clock function. The clock_t type is an arithmetic
    type capable of representing times; and whose range and precision
    of times is implementation-defined.
    SEE: http://www.eskimo.com/~scs/C-faq/q19.37.html

    > eg:

    For a standard C code:
    > #include<time.h>
    > //other include files

    Yes, you need stdio.h

    > void main()

    int main(void)
    SEE: http://www.eskimo.com/~scs/C-faq/q11.15.html
    > {clock_t start, end;
    >
    > //codes................
    >
    > start=clock();
    > //your funcrion calls
    > end =clock();
    > printf("\n Time taken is %f seconds.\n",(end-start)/CLK_TCK);


    Use CLOCKS_PER_SEC as mentioned above.

    >

    return 0;
    > }
    >
    > for more information use the help files in your compiler. (I used the
    > Turbo c++ compiler for this).
    >

    For this NG, the absolute resource is the ANSI C STANDARD.
    SEE: http://www.eskimo.com/~scs/C-faq/q11.2.html

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Jan 25, 2005
    #11
  12. Sanda wrote:
    > You can use the clock() function to calculate the time. Just call it
    > before and after the function calls and substract the two values and
    > devide it buy CLK_TCK which is the systems pulse time.
    >
    > eg:
    > #include<time.h>


    #include <stdio.h>

    > //other include files
    >
    > void main()


    UB. int main()

    > {clock_t start, end;
    >


    style?

    > //codes................
    >
    > start=clock();
    > //your funcrion calls
    > end =clock();
    > printf("\n Time taken is %f seconds.\n",(end-start)/CLK_TCK);
    >


    without stdio.h, UB.

    printf (
    "\n Time taken is %f seconds.\n",(double)(end-start)/CLOCKS_PER_SEC
    );

    >
    > }


    return 0;

    > for more information use the help files in your compiler. (I used the
    > Turbo c++ compiler for this).
    >


    The Turbo C++ compiler is a C++ compiler. Use GCC or something
    that supports the C Standard.

    /*
    * @file: interval.c
    */
    #include <stdio.h>
    #include <limits.h>
    #include <time.h>

    int
    main (int argc, char **argv)
    {
    clock_t start, /* start of timing */
    end; /* end of timing */
    int i; /* index */

    /* (A)
    * Start timing
    */
    start = clock ();

    /* (B)
    * the code to time goes here.
    * sample follows (warning:
    * this may take a long time!):
    */
    for (i = 0; i < INT_MAX; ++i)
    printf ("%d\r", i);

    /* (C)
    * end timing
    */
    end = clock ();

    /* (D)
    * display interval
    */
    printf ("\nInterval: %.2f\n", (double)(end-start)/CLOCKS_PER_SEC);

    return 0;
    }

    Regards,
    Jonathan.

    --
    "Women should come with documentation." - Dave
     
    Jonathan Burd, Jan 25, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Cameron

    Time taken algorithm

    Ben Cameron, Aug 4, 2004, in forum: Java
    Replies:
    4
    Views:
    614
    P.Hill
    Aug 5, 2004
  2. Ike
    Replies:
    3
    Views:
    1,103
    opalinski from opalpaweb
    Jan 23, 2006
  3. Shriphani
    Replies:
    2
    Views:
    572
    Ian Kelly
    Jun 1, 2008
  4. Alf P. Steinbach
    Replies:
    2
    Views:
    1,485
    John H.
    Jan 11, 2010
  5. Balog Pal
    Replies:
    2
    Views:
    364
    Balog Pal
    Jan 11, 2010
Loading...

Share This Page