1753727 Members
4623 Online
108799 Solutions
New Discussion юеВ

Re: time precision

 
SOLVED
Go to solution

time precision

I am looking for a way to get the greatest time precision that I can from my HP-UX 11.23 Itanium system. We have found a link to a resource that leads us to believe that we can get down to the nano second level: http://h21007.www2.hp.com/portal/download/files/unprot/hpux/HowToTellTheTime.pdf

However this references C functions and I am not a C programmer. Does anyone know of a different way to get this level of precision or can someone write a simple C program that will use the correct C function to display the current timestamp down to the nano second?

Points will be awarded.
6 REPLIES 6
Steven Schweda
Honored Contributor

Re: time precision

> [...] I am not a C programmer.

Then what do you intend to do with the
high-precision time value? Display it for a
person who can't read it in a million
nanoseconds? What's the point?

Is there some actual problem which you are
trying to solve?

Note also that precision and accuracy are not
the same thing.
A. Clay Stephenson
Acclaimed Contributor

Re: time precision

If you stop and think about this for a moment, you will realize how ludicrous this request is. Very high resolution timers only have meaning inside a single application so that unless your measurements are inside a C program (or Java possibly), the measurements will mean little. Trying to call a high resolution timer externally makes no sense because the overhead of fork()'ing and exec()'ing a new process will swamp the resolution of your timer. Moreover, the non-repeatability of such external calls will gretly exceed the resolution of your timer by orders of magnitude.

It would help if you describe what you are trying to do and then more reasonable schemes might be suggested.
If it ain't broke, I can fix that.

Re: time precision

I'm not sure how the application of this will help with the problem, but we have an application with a database that has over 10K connections. Many events occur at or near simultaneous sequence. We are trying to create an audit database that will timestamp each transaction - with as great a precision as possible. Our DBAs were having troubles generating this information and asked us to look into it to see if the information is even available. The intent of my request here would not be to use any external program in this process, but just gain some understanding on how this function works through a simple program.
Steven Schweda
Honored Contributor

Re: time precision

> I'm not sure how the application of this
> will help with the problem, [...]

That's ok. I'm not sure how the answer to
your question will help with the application.

> [...] gain some understanding on how this
> function works through a simple program.

I'd assume that it works about as documented.
Let's imagine that you have a working example
program in hand. What will you (or your
DBAs) do with it?

I can see how an event sequence number could
be useful in establishing the order of a set
of events. It's less clear to me how a
super-precise time value obtained by some
highly questionable method will do anything
useful.

> Points will be awarded.

Oh, yeah. I can see that, all right.
Dennis Handly
Acclaimed Contributor
Solution

Re: time precision

>but just gain some understanding on how this function works through a simple program.

Colin's procedure should work fine.

> Steven: It's less clear to me how a super-precise time value obtained by some highly questionable method will do anything useful.

Why do you think Colin's "Generating Timestamps" is questionable? Or are you reading the fine print and it seems complex to do and there is a lot of hand waving?

(I was with Colin on a customer visit when we found the problems in "Manipulating and Formatting Times".)

>can someone write a simple C program that will use the correct C function to display the current timestamp down to the nano second?

Something like this?

#include
#include
void foo() {}
struct timeval start;
int main() {
int i;
hrtime_t now, later, nano;
struct tm *p;
gettimeofday(&start, NULL);
now = gethrtime();
for (i = 0; i < 1000000; ++i)
foo();
later = gethrtime();
later -= now; // delta
printf("nano time diff: %lld\n", later);
p = localtime(&start.tv_sec);
printf("The starting time was: %.19s .%09lld secs\n",
asctime(p), start.tv_usec * 1000LL);

nano = start.tv_usec * 1000LL + later;
start.tv_sec += nano / 1000000000LL;
nano %= 1000000000LL;
p = localtime(&start.tv_sec);
printf("The starting time was: %.19s .%09lld secs\n",
asctime(p), nano);
return 0;
}

As Colin mentions, for this to work properly these two values must be known/shared among all threads/processes trying to do this timestamping
gettimeofday(&start, NULL);
now = gethrtime();

Re: time precision

Dennis,
Thank you for the code.