Re: Subtracting Microseconds from Timestamp
- From: Valentin Nechayev <netch@xxxxxxxxxxxxxxxx>
- Date: Thu, 3 Sep 2009 09:56:26 +0300
dev/null wrote:
dn> Hello, I'm a UNIX newbie. I'm trying to get the time elapse between
dn> some queries. I have a timestamp, and then another time in
dn> miliseconds. I'm having trouble getting the difference. For example,
dn> initial time is 1251935975
This timestamp form (I guess it's unixtime in seconds) doesn't contain
parts of seconds at all, so isn't suitable to measure intervals with
required precision.
You should use gettimeofday() or clock_gettime() which also give parts
of seconds, accordingly in microseconds or nanoseconds.
dn> but the microseconds I'm given after the
dn> query is 80192300.
If this is really microseconds count, it means ~80 seconds, but I
don't know call which gives this.
If you need to get interval between two queries, use the same request
type and calculate difference. In userland, you can use real values
for this:
#include <sys/time.h>
double etime(void)
{
struct timeval tv;
gettimeofday(&tv, NULL); // it's _quite_ unlikely to fail
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
Then, you can simply do
t0 = etime();
do a query;
t1 = etime();
printf("Elapsed time: %g\n", t1 - t0);
--netch--
.
- References:
- Subtracting Microseconds from Timestamp
- From: dev/null
- Subtracting Microseconds from Timestamp
- Prev by Date: Re: Subtracting Microseconds from Timestamp
- Next by Date: Re: Got "permission denied" when creating a posix message queue.
- Previous by thread: Re: Subtracting Microseconds from Timestamp
- Next by thread: Re: Subtracting Microseconds from Timestamp
- Index(es):
Relevant Pages
|