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



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print largest and smallest number. amp10388 UNIX for Dummies Questions & Answers 2 05-07-2008 10:28 AM
checking the smallest and largest number subin_bala Shell Programming and Scripting 4 04-24-2008 07:32 AM
DNS name resolution e250user SUN Solaris 1 07-25-2006 11:13 AM
TCP/IP name resolution progressdll UNIX for Advanced & Expert Users 2 05-07-2002 11:33 AM
Resolution..?? Linux_fan UNIX Desktop for Dummies Questions & Answers 3 03-06-2002 11:33 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-06-2008
harsha10 harsha10 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 8
smallest resolution using gettimeofday() in C

Hello ...
Can somebody help telling me how smallest clock resolution on ones sytem can be in obtained in C using gettimeofday() .

Thankz in advance...
  #2 (permalink)  
Old 02-06-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 741
Quote:
Originally Posted by harsha10 View Post
Hello ...
Can somebody help telling me how smallest clock resolution on ones sytem can be in obtained in C using gettimeofday() .

Thankz in advance...
The smallest resolution for gettimeofday would be microseconds according to the manpage.
  #3 (permalink)  
Old 02-06-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,100
1 microsecond is the granularity of the system call, not the resolution. To obtain the resolution you should use clock_getres() if you have it. Unix systems without clock_getres() tend to have a resolution of 10 milliseconds. There is no reliable way to obtain the resolution with gettimeofday().
  #4 (permalink)  
Old 02-07-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 741
Question

Quote:
Originally Posted by harsha10 View Post
Hello ...
Can somebody help telling me how smallest clock resolution on ones sytem can be in obtained in C using gettimeofday() .

Thankz in advance...
gettimeofday will give you the current date and time specified in seconds or microseconds from the UNIX Epoch but not the resolution of the system-wide clock so be specific as to what you want?
  #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
}
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0