[lug] High resolution timers

Steve Sullivan sullivan at Mathcom.COM
Sat Feb 22 13:14:17 MST 2003


Thanks for the note on gettimeofday and for the test code.
Yes, it works.  Here's a modification of the demo that
shows the timer is updated about once every microsecond on
an intel P-800.  I cache the deltas to an array so there's
no question that perhaps printf is taking too long.


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

// g++ -Wall -pedantic -o tgettimeofday tgettimeofday.c

#define STATSLEN (1000*1000)

int main( int argc, char **argv ) {
	double stats[ STATSLEN];
	struct timeval tv, prevtv;
	gettimeofday( &prevtv, NULL);
	long istat;
	for (istat = 0; istat < STATSLEN; istat++) {
		gettimeofday( &tv, NULL);
		double delta = tv.tv_sec - prevtv.tv_sec
			+ 1.e-6 * (tv.tv_usec - prevtv.tv_usec);
		stats[istat] = delta;
		prevtv.tv_sec = tv.tv_sec;
		prevtv.tv_usec = tv.tv_usec;
	}
	for (istat = 0; istat < STATSLEN; istat++) {
		printf("delta: %g\n", stats[istat]);
	}
	return 0;
}

The output:

delta: 1e-06
delta: 2e-06
delta: 1e-06
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
delta: 1e-06
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
delta: 1e-06
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
delta: 1e-06
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
delta: 1e-06
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
delta: 1e-06
delta: 1e-06
delta: 0
delta: 1e-06
delta: 0
...


Thanks!

Steve

========================================
Steve Sullivan    sullivan at mathcom.com

   Mathcom Solutions Inc.: Custom Software Development.
    * Mathematical optimization, simulation, and modeling.
    * Data mining, information retrieval.
    * Java, XML, C++, Mathematica, Matlab, XSLT, XQuery, SOAP, RMI, ...

http://www.mathcom.com    303-494-7115
========================================



More information about the LUG mailing list