Operating System - HP-UX
1751878 Members
5333 Online
108782 Solutions
New Discussion юеВ

Re: time in milliseconds _ Urgent help required

 
SOLVED
Go to solution
Sathish C
Frequent Advisor

time in milliseconds _ Urgent help required

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

timenow=msecond(&time);
//some actions//
timafter=msecond(&time);
Please help me as this is knida urgent

Thank you
Some cause happiness wherever they go; others, whenever they go
5 REPLIES 5
Kiran Kumar M
Advisor
Solution

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

Regards,
Jean-Luc
fiat lux
Jean-Luc Oudart
Honored Contributor

Re: time in milliseconds _ Urgent help required

or try based on this small pgm

#include
#include
#include


main()
{
int tim1,tim2;
struct timeval tp1,tp2;
struct timezone tzp;
tim1=gettimeofday(&tp1,&tzp);
(void) usleep(200000); /* 200ms */
tim2=gettimeofday(&tp2,&tzp);
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);
}

Regards,
Jean-Luc
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

#include

struct timeval now;
struct timezone timezone;


gettimeofday(&now,&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.

Enjoy
Rossco

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