The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #5 (permalink)  
Old 06-19-2009
iozone iozone is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 1
Get clock resolutoin

/*
* Print the gettimeofday() clock resolution for this machine.
*
* Code taken from Iozone. Iozone Filesystem Benchmark
* Author: Don Capps
*
*/
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#define THISVERSION " Version $Revision: 1.2 $"
double time_res,delay;
void get_resolution(); /* Works with most compilers */
static double time_so_far1(); /* Works with most compilers */
/*
* Measure and print the gettimeofday() resolution.
*/
main()
{
printf("\n\nMeasuring the gettimeofday() resolution\n\n");
get_resolution();
printf("Time resolution of gettimeofday() = %f seconds\n",time_res);
printf("Time resolution of gettimeofday() = %f milli seconds\n",
time_res*1000);
printf("Time resolution of gettimeofday() = %f micro seconds\n\n",
time_res*(1000*1000));
printf("Provided, courtesy of Iozone. http://www.iozone.org\n\n");
}
/*
* Lifted code from Iozone.
*/
#ifdef HAVE_ANSIC_C
void
get_resolution(void)
#else
void
get_resolution()
#endif
{
double starttime, finishtime;
long j;
again:
finishtime=time_so_far1(); /* Warm up the instruction cache */
starttime=time_so_far1(); /* Warm up the instruction cache */
delay=j=0; /* Warm up the data cache */
while(1)
{
starttime=time_so_far1();
for(j=0;j< delay;j++)
;
finishtime=time_so_far1();
if(starttime==finishtime)
delay++;
else
break;
}
time_res = (finishtime-starttime)/1000000.0;
}
/*
* Lifted code from Iozone.
*/
/************************************************************************/
/* Time measurement routines. */
/* Return time in microseconds */
/************************************************************************/
#ifdef HAVE_ANSIC_C
static double
time_so_far1(void)
#else
static double
time_so_far1()
#endif
{
/* For Windows the time_of_day() is useless. It increments in 55 milli second */
/* increments. By using the Win32api one can get access to the high performance */
/* measurement interfaces. With this one can get back into the 8 to 9 */
/* microsecond resolution. */
#ifdef Windows
LARGE_INTEGER freq,counter;
double wintime;
double bigcounter;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&counter);
bigcounter=(double)counter.HighPart *(double)0xffffffff +
(double)counter.LowPart;
wintime = (double)(bigcounter/(double)freq.LowPart);
return((double)wintime*1000000.0);
#else
#if defined (OSFV4) || defined(OSFV3) || defined(OSFV5)
struct timespec gp;
if (getclock(TIMEOFDAY, (struct timespec *) &gp) == -1)
perror("getclock");
return (( (double) (gp.tv_sec)*1000000.0) +
( ((float)(gp.tv_nsec)) * 0.001 ));
#else
struct timeval tp;
if (gettimeofday(&tp, (struct timezone *) NULL) == -1)
perror("gettimeofday");
return ((double) (tp.tv_sec)*1000000.0) +
(((double) tp.tv_usec) );
#endif
#endif
}