1756076 Members
4075 Online
108840 Solutions
New Discussion юеВ

nanosecond

 
SOLVED
Go to solution
isopkim_2
New Member

nanosecond

Hi. Admins.
Is there any method to get nanosecond in the HP?
I used "gethrtime" and "clock_gettime", but they only returned "microsecond"..
please, Help me..
Thanks in advance.
3 REPLIES 3
Jean-Louis Phelix
Honored Contributor

Re: nanosecond

hi,

Perhaps have you been too fast when reading clock_gettime man page. It seems to be able to manage nanoseconds. You even have an example :

#include
#include
struct timespec resolution;
if (clock_getres(CLOCK_PROFILE, &resolution)) {
perror("clock_getres(CLOCK_PROFILE) failed");
exit(1);
}
(void)printf("Resolution of user profiling clock is:\n");
(void)printf("%d seconds and %d nanoseconds.\n",
resolution.tv_sec, resolution.tv_nsec);

Regards.
It works for me (┬й Bill McNAMARA ...)
isopkim_2
New Member

Re: nanosecond

Thanks to your answer. but,
I think i should have explained my questions more detail..
My problem is that when I wrote program like below
------------------------------------------
for ( i=0;i<10;i++){
if (clock_gettime(CLOCK_REALTIME, &cur_time)) {
perror("clock_gettime(CLOCK_REALTIME) failed");
exit(1);
}
printf("%ld nsec\n", cur_time.tv_nsec );

the result looks like below.
--------------------
143781000 nsec
143811000 nsec
143839000 nsec
143868000 nsec
143896000 nsec
143923000 nsec
143951000 nsec
143978000 nsec
144006000 nsec
144033000 nsec

The last three digits are always "0"
Do i have to tun some kernel parameters?
Jean-Louis Phelix
Honored Contributor
Solution

Re: nanosecond

Hi,

You seem to be right. I think that it's related to clock resolution too high. But gethrtime() works better although it's not related to time of day. Using these progs :

A - no sleep() :
#include
main()
{
int i;
for ( i=0;i<3;i++)
{
printf("%lld nsec\n", gethrtime());
}
}
home> myprog
6652778157318723 nsec
6652778158086617 nsec
6652778158111862 nsec

B - with sleep() :
#include
#include
main()
{
int i;
for ( i=0;i<3;i++, sleep(1))
{
printf("%lld nsec\n", gethrtime());
}
}
home > myprog
6652972898147057 nsec
6652973900562902 nsec
6652974910552897 nsec

I can really see increments around 1 second. Be careful to use %lld !!!

Regards.

It works for me (┬й Bill McNAMARA ...)