calculate execution time

Discussion in 'C Programming' started by Tim Quon, Aug 28, 2003.

  1. Tim Quon

    Tim Quon Guest

    Hi

    Is there any function to get the current time so I can calculate the
    execution time of my code?

    What all is in the time.h and sys/times.h?

    Thanks
    Tim
     
    Tim Quon, Aug 28, 2003
    #1
    1. Advertising

  2. Tim Quon

    Aggro Guest

    Tim Quon wrote:

    > Hi
    >
    > Is there any function to get the current time so I can calculate the
    > execution time of my code?


    If you need to get more accurate time than 1 second(which usually is the
    case), you need to use something else than standard C.

    time(0) returns you the current time in seconds. (#include <time.h>)

    You can take the time at the start and at the end of your program and
    calculate the execution time in seconds from that.
     
    Aggro, Aug 28, 2003
    #2
    1. Advertising

  3. Tim Quon wrote:

    > On Thu, 28 Aug 2003 13:19:24 GMT, Aggro <>
    > wrote:
    >
    >
    >>Tim Quon wrote:
    >>
    >>
    >>>Hi
    >>>
    >>>Is there any function to get the current time so I can calculate the
    >>>execution time of my code?

    >>
    >>If you need to get more accurate time than 1 second(which usually is the
    >>case), you need to use something else than standard C.
    >>
    >>time(0) returns you the current time in seconds. (#include <time.h>)
    >>
    >>You can take the time at the start and at the end of your program and
    >>calculate the execution time in seconds from that.

    >
    >
    > What library can I use to get more accurate than 1 second?


    Try a platform specific library or some operating system functions.
    There is no crime in using operating system functions; just remember
    that your program may no longer be portable to other platforms.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Aug 28, 2003
    #3
  4. Tim Quon

    Tim Prince Guest

    Tim Quon wrote:

    > On Thu, 28 Aug 2003 13:19:24 GMT, Aggro <>
    > wrote:
    >
    >>Tim Quon wrote:
    >>
    >>> Hi
    >>>
    >>> Is there any function to get the current time so I can calculate the
    >>> execution time of my code?

    >>
    >>If you need to get more accurate time than 1 second(which usually is the
    >>case), you need to use something else than standard C.
    >>
    >>time(0) returns you the current time in seconds. (#include <time.h>)
    >>
    >>You can take the time at the start and at the end of your program and
    >>calculate the execution time in seconds from that.

    >
    > What library can I use to get more accurate than 1 second?

    Time to consult the FAQ, your favorite C texts, your library documentation.
    clock() is standard C, is intended for timing execution (by CPU time, not
    "wall time," and usually has 0.01 second resolution. <OT> gettimeofday() is
    not standard C, but on a good quality library will give you at least
    millisecond resolution. Popular OS have non-portable alternatives.
    --
    Tim Prince
     
    Tim Prince, Aug 28, 2003
    #4
  5. Tim Quon

    Kevin Bracey Guest

    In message <>
    Tim Quon <> wrote:

    > Is there any function to get the current time so I can calculate the
    > execution time of my code?


    If you want a time measurement for speed testing, rather than the time of
    day, then the standard C function clock() in <time.h> will do you, with the
    added benefit that it gives "processor time used" rather than absolute time,
    which may be what you really want in a multitasking environment. Precision is
    implementation dependent - typically centiseconds or milliseconds.

    On a POSIX system, gettimeofday() in <sys/time.h> may provide higher
    precision - you should ask in a platform-specific group to get better advice
    on the best solution for you.

    --
    Kevin Bracey, Principal Software Engineer
    Tematic Ltd Tel: +44 (0) 1223 503464
    182-190 Newmarket Road Fax: +44 (0) 1223 503458
    Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
     
    Kevin Bracey, Aug 28, 2003
    #5
  6. Tim Quon <> wrote in message news:<>...
    > Hi
    >
    > Is there any function to get the current time so I can calculate the
    > execution time of my code?
    >
    > What all is in the time.h and sys/times.h?
    >
    > Thanks
    > Tim


    If you want more accurate time statistics then ur program, obviously,
    should run under the supervision of some other program. THe other
    program tracks the start and end of ur program. In Linux kind of
    environment u have command "time"itself. for more on time u can do
    "man time"

    --Prashant Adkoli.
     
    Prashant Adkoli, Aug 28, 2003
    #6
  7. Tim Quon

    Tim Hagan Guest

    Prashant Adkoli wrote:
    >
    > Tim Quon <> wrote in message news:<>...
    > > Hi
    > >
    > > Is there any function to get the current time so I can calculate the
    > > execution time of my code?
    > >
    > > What all is in the time.h and sys/times.h?
    > >
    > > Thanks
    > > Tim

    >
    > If you want more accurate time statistics then ur program, obviously,

    ^^^
    u missed one :) SCNR

    > should run under the supervision of some other program. THe other
    > program tracks the start and end of ur program. In Linux kind of
    > environment u have command "time"itself. for more on time u can do
    > "man time"


    --
    Tim Hagan
     
    Tim Hagan, Aug 28, 2003
    #7
  8. "Tim Quon" <> a écrit dans le message de news:
    ...
    > Hi
    >
    > Is there any function to get the current time so I can calculate the
    > execution time of my code?
    >
    > What all is in the time.h and sys/times.h?
    >
    > Thanks
    > Tim



    You could try a profiling tool such as gprof(linux). It will give you all
    timing statistics about your application and the functions in it.
     
    Patrick Jeanson, Sep 3, 2003
    #8
    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. TomislaW
    Replies:
    2
    Views:
    4,232
    James Moore
    Nov 10, 2004
  2. =?Utf-8?B?YXNhZA==?=

    calculate page execution time

    =?Utf-8?B?YXNhZA==?=, Sep 26, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    515
    Peter Rilling
    Sep 26, 2005
  3. Magesh
    Replies:
    8
    Views:
    498
    Keith Thompson
    Oct 10, 2007
  4. Magesh
    Replies:
    1
    Views:
    320
    Ian Collins
    Sep 28, 2007
  5. Ruwan Budha
    Replies:
    4
    Views:
    1,201
    Kirk Haines
    Mar 9, 2011
Loading...

Share This Page