smallest resolution using gettimeofday() in C


 
Thread Tools Search this Thread
Top Forums Programming smallest resolution using gettimeofday() in C
# 1  
Old 02-06-2008
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  
Old 02-06-2008
Quote:
Originally Posted by harsha10
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  
Old 02-06-2008
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  
Old 02-07-2008
Question

Quote:
Originally Posted by harsha10
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  
Old 06-19-2009
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
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with keep the smallest record in file

Input file US Score 10 UK Ball 20 AS Score 50 AK Ball 10 PZ Ballon 50 PA Score 70 WT Data 10 . . Desired output file US Score 10 AK Ball 10 WT Data 10 . . (2 Replies)
Discussion started by: perl_beginner
2 Replies

2. Shell Programming and Scripting

Find smallest between replicates ID

Hi All I need to find the smallest values between replicates id (column1) Input file: a name1 1200 a name2 800 b name1 100 b name2 150 b name3 4output: a name2 800 b name3 4 Do you have any suggestion? Thank you! (9 Replies)
Discussion started by: giuliangiuseppe
9 Replies

3. Shell Programming and Scripting

Find smallest & largest in every column

Dear All, I have input like this, J_15TEST_ASH05_33A22.13885.txt: $$ 1 MAKE SP1501 1 1 4 6101 7392 2 2442 2685 18 3201 4008 20 120 4158 J_15TEST_ASH05_33A22.13885.txt: $$ 1 MAKE SP1502 1 1 4 5125 6416 2 ... (4 Replies)
Discussion started by: attila
4 Replies

4. Shell Programming and Scripting

Find the smallest block

Hi, Here's my data - aa bb cc aa dd ee Now I need to find the smallest block surrounded by aa & dd. Following is not helpful - sed -n '/aa/,/dd/p' file I need only - aa dd (1 Reply)
Discussion started by: nexional
1 Replies

5. UNIX for Dummies Questions & Answers

Smallest UNIX operating system

Which is the smallest Unix operating system and how do I download it? edit by bakunin: corrected typo in the thread-title and removed the text formatting: we are able to read non-bold text either. (4 Replies)
Discussion started by: Nalcolmshally
4 Replies

6. Shell Programming and Scripting

AWK (how) to get smallest/largest nr of ls -la

Hey, This is a long-shot however, I am stuck with the following problem: I have the output from ls -la, and I want to sort some of that data out by using AWK to filter it. ls -la | awk -f scriptname.awk Input: For example: drwxr-xr-x 3 user users 4096 2010-03-14 20:15 bin/... (5 Replies)
Discussion started by: abciscool
5 Replies

7. Programming

Gettimeofday problem

i have written the code in which i want to calculate timedifference of request sent time and response receive time from device in second and microsecond. but when i executes the binary i get the response receive time earlier than request sent time which in turn returns the negative time difference.... (3 Replies)
Discussion started by: kailas.awchar
3 Replies

8. Programming

the problem of gettimeofday

Hi all, I just wrote a small problem to check gettimeofday in a multi-cores environment. int timediff(double fTimeQvs, double fTimeTcp) { int iTimeDiff; iTimeDiff = (((((int)fTimeQvs)/10000) - (((int)fTimeTcp)/10000)) * 3600) * 1000000; iTimeDiff +=... (17 Replies)
Discussion started by: dophine
17 Replies

9. UNIX for Dummies Questions & Answers

How to print largest and smallest number.

Hey. This is pretty easy stuff but I'm learning the basics of Unix at the moment so keep that in mind. I have to: 1) Write a C-shell script to monitor user activity on the server for 13 minutes. 2) Then print the smallest and largest number of users during these 13 minutes. I have this: 1)... (2 Replies)
Discussion started by: amp10388
2 Replies

10. Shell Programming and Scripting

checking the smallest and largest number

Hi All, My script is reading a log file line by line log file is like ; 19:40:22 :INFO Total time taken to Service External Request---115ms 19:40:25 DEBUG : Batch processed libdaemon.x86_64 0-0.10-5.el5 - u 19:40:22 INFO Total time taken to Service External Request---20ms 19:40:24... (4 Replies)
Discussion started by: subin_bala
4 Replies
Login or Register to Ask a Question