views:

854

answers:

3

How can I express 10 milliseconds using timeval?

This is what I have so far:

struct timeval now;
now.tv_usec =10000;
+12  A: 

A struct timeval represents a time as a number of seconds (tv_sec) plus a number of microseconds (tv_usec) between 0 and 1,000,000. Thus, to represent 10 milliseconds, you would use 10,000 microseconds, as you suggested:

struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 10000;
Adam Rosenfield
+1 for setting both fields of the time.
RBerteig
+4  A: 

it's

 struct timeval {
    int tv_sec;    // seconds 
    int tv_usec;   // microseconds!

so now.

tv_sec = 0;
tv_usec = 10000;

` would be right

Silfverstrom
Except that the tv_sec field is (probably) uninitialized and hence likely not zero. It would be safer to set it explicitly to zero so that the intent is clear.
RBerteig
But, in this case, tv_sec would not be properly initialized, which may create problems. Either memset() or assign zero to tv_sec.
bortzmeyer
yeah i agree, fixed it.
Silfverstrom
A: 

i need to knw execution time of my program which writen in c languge at linux enviornment/....pls any one help..itz urgent......

jomjp