timer function

N

Nishu

Hi All,

I wanted to profile my function.

I've two choices.
1. using time.h and clock() function.
2. using sys/time.h and gettimeofday() function.

Could you help me to know which one is better in precision (advantages/
disadvantages) and under what circumstances one should use which
function?

My observation :
I've used both functions for profiling. clock() doesnt look very
precise so I took median of 7 readings and for gettimeofday() I took
mean of 5 readings. Both comes almost same. But my knowledge is half
and I dont know the answer of my query.

Thanks,
Nishu
 
R

Richard Bos

Nishu said:
I wanted to profile my function.

I've two choices.
1. using time.h and clock() function.
2. using sys/time.h and gettimeofday() function.

Could you help me to know which one is better in precision (advantages/
disadvantages) and under what circumstances one should use which
function?

The former is ISO C. The latter is not, and therefore off-topic here.
clock() has a guaranteed precision of exactly zilch; what precision
gettimeofday() has is off-topic here, so ask in a newsgroup which
specialises in the compiler and/or OS you're using. When you do, you'll
probably be told to use something else entirely.

For matters involving time with high precision, it is almost always best
to use a system-specific solution. For matters involving only rough
estimations of processor time, _or_ only the date and time down to a
second, and within half a century from today, use the ISO C functions in
<time.h>.

Richard
 
N

Nishu

The former is ISO C. The latter is not, and therefore off-topic here.
clock() has a guaranteed precision of exactly zilch; what precision
gettimeofday() has is off-topic here, so ask in a newsgroup which
specialises in the compiler and/or OS you're using. When you do, you'll
probably be told to use something else entirely.

For matters involving time with high precision, it is almost always best
to use a system-specific solution. For matters involving only rough
estimations of processor time, _or_ only the date and time down to a
second, and within half a century from today, use the ISO C functions in
<time.h>.

O.K. Thanks. :)
 
A

Army1987

For matters involving time with high precision, it is almost always best
to use a system-specific solution. For matters involving only rough
estimations of processor time, _or_ only the date and time down to a
second, and within half a century from today, use the ISO C functions in
<time.h>.
For sufficiently small values of half a century. On my system I
think time() is going to break in about 31 years.
 
D

Duncan Muirhead

O.K. Thanks. :)
This is off topic but I've found this useful:
#include <sys/time.h>
#include <sys/resource.h>
double second( void)
{
struct rusage ru;
getrusage(RUSAGE_SELF,&ru) ;
return
(double) (ru.ru_utime.tv_sec+ru.ru_stime.tv_sec)
+ ((double) (ru.ru_utime.tv_usec+ru.ru_stime.tv_usec))*1.0e-6
;
}
This returns the processor time that your process has used (or caused
to be used by the system), so far. So
t = second(); your_code(); t = second()-t;
gives the processor time in seconds your_code() took.
Doing a similar thing with eg gettimeofday() will give figures that
will depend on how many other processes are running and on what they are
doing.
 
C

CBFalconer

Nishu said:
I wanted to profile my function.

I've two choices.
1. using time.h and clock() function.
2. using sys/time.h and gettimeofday() function.

No you don't, not here. Your only choice is means that are
specified in the C standard. This automatically eliminates #2.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top