Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Showing results for 
Search instead for 
Did you mean: 

time in milliseconds _ Urgent help required

Go to solution
Sathish C
Frequent Advisor

time in milliseconds _ Urgent help required

I need to find the time in milliseconds using c functions . I need to do something like this

//some actions//
Please help me as this is knida urgent

Thank you
Some cause happiness wherever they go; others, whenever they go

Re: time in milliseconds _ Urgent help required

You could use gettimeofday()..

Just use a macro to convert the output of this function into milliseconds.. here's an example:

#define msecond(&timer) ((gettimeofday(&timer,&dont_care)) ? -1.0 : ((float)(timer.tv_sec * 1000) + (timer.tv_usec/1000) ))

Just subtract timenow from timeafter to get the diff in ms
Jean-Luc Oudart
Honored Contributor

Re: time in milliseconds _ Urgent help required

You can use the "clock" function.
call clock 1st
run the code
call clock again it will give you the difference in microseconds.

man clock

fiat lux
Jean-Luc Oudart
Honored Contributor

Re: time in milliseconds _ Urgent help required

or try based on this small pgm


int tim1,tim2;
struct timeval tp1,tp2;
struct timezone tzp;
(void) usleep(200000); /* 200ms */
printf("first time : %d %d : %d\n",tim1,tp1.tv_sec,tp1.tv_usec);
printf("second time : %d %d : %d\n",tim2,tp2.tv_sec,tp2.tv_usec);

fiat lux
rick jones
Honored Contributor

Re: time in milliseconds _ Urgent help required

If you simply want to measure a relative time - that is measure how long it took to perform some action, you are probably better-off using gethrtime() - which is installed via a patch on 11.11 and perhaps 11.0. It can do relative time measurements without the overhead of gettimeofday(). The units it returns are I believe nanoseconds - a small matter of math.
there is no rest for the wicked yet the virtuous have no pillows
Ross Barton
Occasional Advisor

Re: time in milliseconds _ Urgent help required


struct timeval now;
struct timezone timezone;


/* now.tv_sec is seconds since EPOCH
(1/1/1970 00:00:00) */

/* now.tv_usec is microseconds in the current second */

do a man on "gettimeofday" and have a look in the time.h include file for more info and details of the structures.
Note: Microseconds are not always accurate to the microsecond - there is some small error involved in the CPU calculating this. However, it would be insignificant in the calculation of milliseconds from microseconds. This error can be significant in calculation of timestamps for things like auditing software or flight simulations etc.


Everything should be made as simple as possible, but not simpler. Albert Einstein.