By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

calculate execution time

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

Nov 13 '05 #2

P: n/a
Tim Quon wrote:
On Thu, 28 Aug 2003 13:19:24 GMT, Aggro <sp**********@yahoo.com>
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

Nov 13 '05 #3

P: n/a
Tim Quon wrote:
On Thu, 28 Aug 2003 13:19:24 GMT, Aggro <sp**********@yahoo.com>
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
Nov 13 '05 #4

P: n/a
In message <a8********************************@4ax.com>
Tim Quon <ti***@freesurf.ch> 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/
Nov 13 '05 #5

P: n/a
Tim Quon <ti***@freesurf.ch> wrote in message news:<a8********************************@4ax.com>. ..
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.
Nov 13 '05 #6

P: n/a
Prashant Adkoli wrote:

Tim Quon <ti***@freesurf.ch> wrote in message news:<a8********************************@4ax.com>. ..
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
Nov 13 '05 #7

P: n/a

"Tim Quon" <ti***@freesurf.ch> a écrit dans le message de news:
a8********************************@4ax.com...
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.

Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.