calculate execution time

T

Tim Quon

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
 
A

Aggro

Tim said:
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.
 
T

Thomas Matthews

Tim said:
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
 
T

Tim Prince

Tim said:
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.
 
K

Kevin Bracey

In message <[email protected]>
Tim Quon said:
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.
 
P

Prashant Adkoli

Tim Quon said:
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.
 
P

Patrick Jeanson

Tim Quon said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top