Solved

What is the most accurate timer on a unix system? (c language)

Posted on 2004-04-09
3
496 Views
Last Modified: 2013-12-26
I am simulating the datalink layer between two  entities in a network.

I need to get down to microseconds if possible.

I'm compiling c code on a the gcc compiler.

I basically need to do this.

StartTime = getTime();


loop(   )
{
........
  CurrentTime = getTime();
  if(CurrentTime - StartTime > 50 microseconds)
  {
     do this;
  }
  ......
}


Any ideas on the best timer function to use?

Thank you.


0
Comment
Question by:oxygen_728
3 Comments
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 200 total points
Comment Utility
Although gettimeofday() supports microsecond resolution, the clocks on most
machines do not tick more than a few hundred times per second, causing the
returned microsecond values from gettimeofday() to jump by large quanta.
0
 
LVL 3

Accepted Solution

by:
idt earned 300 total points
Comment Utility
Like Brett.. said, gettimeofday supports usec resolution, but diiferent systems may jump by large increments, run the following test app to test your system.

#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>

/*
const char* TimeToString(const struct tm* t) {
  static char buf[30];
  strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",t);
  return buf;
}

const char* FormatTime(const struct timeval* v) {
  static char buf[128];
  struct tm t;
  localtime_r(&v->tv_sec, &t);
  sprintf(buf,"%s.%ld", TimeToString(&t),v->tv_usec);
  return buf;
}
*/

void difftimeval(const struct timeval* v, const struct timeval* w, struct timeval *r) {
  r->tv_sec=(v->tv_sec - w->tv_sec);
  r->tv_usec=(v->tv_usec - w->tv_usec);
  return;
}

int main(void) {
  int i;
  struct timeval v,w,r;
  gettimeofday(&w,0);
  for(i=0;i<10;i++) {
    gettimeofday(&v,0);
    difftimeval(&v,&w,&r);
    //printf("time is now:%s  elapsed:%ld seconds, %ld usecs\n",FormatTime(&v),r.tv_sec,r.tv_usec);
    printf("elapsed:%ld seconds, %ld usecs\n",r.tv_sec,r.tv_usec);
    memcpy(&w,&v,sizeof(v));
  }
  return 0;
}

My output is:

elapsed:0 seconds, 1 usecs
elapsed:0 seconds, 56 usecs
elapsed:0 seconds, 4 usecs
elapsed:0 seconds, 4 usecs
elapsed:0 seconds, 3 usecs
elapsed:0 seconds, 4 usecs
elapsed:0 seconds, 3 usecs
elapsed:0 seconds, 4 usecs
elapsed:0 seconds, 3 usecs
elapsed:0 seconds, 3 usecs

Unremark for more verbose date/time output
time is now:2004-04-10 00:17:53.583650  elapsed:0 seconds, 1 usecs
time is now:2004-04-10 00:17:53.583863  elapsed:0 seconds, 213 usecs
time is now:2004-04-10 00:17:53.583872  elapsed:0 seconds, 9 usecs
time is now:2004-04-10 00:17:53.583880  elapsed:0 seconds, 8 usecs
time is now:2004-04-10 00:17:53.583888  elapsed:0 seconds, 8 usecs
time is now:2004-04-10 00:17:53.583895  elapsed:0 seconds, 7 usecs
time is now:2004-04-10 00:17:53.583902  elapsed:0 seconds, 7 usecs
time is now:2004-04-10 00:17:53.583909  elapsed:0 seconds, 7 usecs
time is now:2004-04-10 00:17:53.583917  elapsed:0 seconds, 8 usecs
time is now:2004-04-10 00:17:53.583924  elapsed:0 seconds, 7 usecs

compiled with gcc version 3.2.2 (Mandrake Linux 9.1 3.2.2-3mdk)

Daniel
0
 

Author Comment

by:oxygen_728
Comment Utility
Thank you both for your time. Have a great day both of you!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to monitor remote shell execution on linux 9 92
format the code in java 6 72
move a line in eclipse 3 60
matchUp  challenge 6 48
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now