• C

milli (or micro) second timing

I'm looking to timestamp information in a program I'm working on. The timestamp will be compared to the current time to determine how much time has gone by. What I'm looking for is something like time(&tloc), only on the scale of milli or micro seconds. Like time gives me the number of seconds since 00:00:00 1970. Is there something that will give me the number of milliseconds since 00:00:00 1980? or since the system has been booted? Some kinda of fixed point for the system so that it can tell how much time has passed on a much finer scale than seconds.
phirephlyAsked:
Who is Participating?
 
griesshCommented:
Then why don't you like my gettimeofday() function? Have you tried to read about it? It gives you the miliseconds and seconds since 1. January 1970 an dyou can subtract, store them as unique timestamps ...
That's what a timestamp is!

======
Werner
0
 
phirephlyAuthor Commented:
oh yes, and there will probably be many timestamps floating around and some might get lost, so something like a process timer or the like won't work. It has to give me a number.
0
 
griesshCommented:
What OS/compiler are you using?

======
Werner
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
phirephlyAuthor Commented:
Unixes, and gcc, but if there's something hat's even more universal, I'd like that. thanks!
0
 
AxterCommented:
Use POSIX functions.
0
 
AxterCommented:
getitimer() is a POSIX function that should work with gcc on unix and linux.
0
 
AxterCommented:
I don't have any example code for getitimer function, but I do have it for setitimer function.
See the following code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
static bool Sleep_Timer_Handler_Set;
//***********************************************************************
//
// PROCEDURE:                    Sleep_Timer_Handler() function
//
// AUTHOR:                         David Maisonave (dmaisona@csc.com)(david@axter.com)    
//
// DESCRIPTION:             Function needed for Sleep() function.
//                                   Used in sigaction()
//
// INPUTS:                  N/A
//
// OUPUTS:                  N/A
//
// SPECIAL CONSIDERATIONS:  Needs static bool Sleep_Timer_Handler_Set variable
//
//***********************************************************************
//
static void Sleep_Timer_Handler(int signo)
{
     Sleep_Timer_Handler_Set=true;
}

//***********************************************************************
//
// PROCEDURE:                    POSIX version of Sleep() function                                  
//
// AUTHOR:                         David Maisonave (dmaisona@csc.com)(david@axter.com)    
//
// DESCRIPTION:             Emulates the Windows Sleep() function using
//                                   POSIX functions.  Sleep() function works like a pause() function
//
// INPUTS:                  Quantity of milliseconds
//
// OUPUTS:                  Always returns zero
//
// SPECIAL CONSIDERATIONS:  Needs above Sleep_Timer_Handler() function
//                                   and static bool Sleep_Timer_Handler_Set variable
//***********************************************************************
//
DWORD Sleep(DWORD dwMilliseconds)
{
     struct sigaction new_sigalrm;
     new_sigalrm.sa_handler = Sleep_Timer_Handler;
     sigemptyset(&new_sigalrm.sa_mask);
     new_sigalrm.sa_flags = 0;
     sigaction(SIGALRM,&new_sigalrm,NULL);
     Sleep_Timer_Handler_Set=false;
     struct itimerval StartTime;
     StartTime.it_interval.tv_sec = 0;
     StartTime.it_interval.tv_usec = 0;
     StartTime.it_value.tv_sec = dwMilliseconds/1000;
     StartTime.it_value.tv_usec = dwMilliseconds%1000;
     struct itimerval OldTime;
     setitimer(ITIMER_REAL,&StartTime,&OldTime);
     while(Sleep_Timer_Handler_Set==false);
     return 0;
}
0
 
griesshCommented:
Good choice Axter, I tempted to suggest gettimeofday(). But I don't know how universal that is.

======
Werner
0
 
AxterCommented:
I was looking up the gettimeofday() function to see if it was part of the POSIX standards, and when I realized that getitimer() is not part of the POSIX standard either.

The correct POSIX standard function is timer_gettime()
This function is in nanoseconds, so it's a better timer.
Here's the list for POSIX timer functions:

#include <signal.h>
#include <time.h>

  int timer_create (
          clockid_t clock_id,
          struct sigevent *evp,
          timer_t *timerid );

  int timer_gettime (
  timer_t timerid,
  struct itimerspec *value);

  int timer_settime (
  timer_t timerid,
  int flags,
  const struct itimerspec *value,
  struct itimerspec *ovalue);

  int timer_delete (
  timer_t timerid);

  int timer_getoverrun (
          timer_t timerid );
0
 
newmangCommented:
If your system supports it you could use gethrtime and sethrvtime to resolve to nanoseconds - its available on Solaris, don't know about others.

Brea in mind that any timer is only as good as the hardware platform it runs on. For example, in some versions of Windows the timing resolution is only good to 55 ms so even though the function promises times of the millisecond resolution in real life you can only really get times in 55ms chunks.
0
 
AxterCommented:
>>gethrtime
That's not a standard function.  Only Solaris supports it, and it's not part of the POSIX or C standards.
0
 
AxterCommented:
phirephly,
Did you forget about this question?
0
 
AxterCommented:
Hi phirephly:
If you have your answer, could you award it.

That way your question can be removed from the unanswered list.

Thanks.

0
 
phirephlyAuthor Commented:
I'm sorry. I tried posting a reply a bit ago, but the website went down without taking it :(  Then over the last couple days, I haven't been able to get on. I hope this post makes it up there. that timer_gettime() and stuff isn't what I'm looking for unless I'm not seeing something. That'll time a function, but how would I get a time stamp out of that?  I want to be able to write out a timestamp to something that can be compared to the current one later. I'll have a lot of these stamps moving around and not all of 'em will return. I just wanna be able to read off the time that's been stamped, subtract it from the now time and go from there. timer_ relies on a reliative start and stop time, I want a solid time that can be output to the screen, written to a file, or whatever I wanna do with it.
0
 
AxterCommented:
Hi phirephly,
I think griessh posted the answer you are looking for.
Could you please post some feedback.

Thanks
0
 
phirephlyAuthor Commented:
alright... if it does secs and msecs, that's what I'm looking for. I would have liked to test it out first, but guess that's my problem I didn't get on it quickly enough. thanks. bye
0
 
AxterCommented:
phirephly,
There's no hurry.  
All you have have to do is let the expert know that you're going to tested, and that you'll get back to him/her after you tested out.
Most experts understand this, and it's a common practice.
If you still have problems with the posted method, just ask the expert on this same post question.
Most experts will follow through with any suggest method.

The more feedback you give the expert the better are your chances that you get exactly what you're looking for, or the next best thing.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.