Subject: Re: timing issue
To: None <netbsd-users@netbsd.org>
From: None <lists@badzong.com>
List: netbsd-users
Date: 11/18/2006 15:54:35
> Could you try a kernel with the NO_TSC_TIME option?

NO_TSC_TIME brings no improvement (gettimeofday gets a bit slower). To
reduce volatility I looped 1000 times over each function. Resluts and
new code below.

Manuel


- without option NO_TSC_TIME ---------------------

timeout:              1000 usec
loops:                1000

gettimeofday:          0.7 usec
nanosleep:         19995.1 usec
usleep:            19999.4 usec
select:            19999.4 usec

--------------------------------------------------


- with option NO_TSC_TIME ------------------------

timeout:              1000 usec
loops:                1000

gettimeofday:          5.4 usec
nanosleep:         19994.4 usec
usleep:            19995.0 usec
select:            19994.9 usec

--------------------------------------------------


- timing.c ---------------------------------------
// Usage: timing [timeout usec] [loops]

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

int main(int argc, char **argv)
{
	struct timeval timer1, timer2, timeout = { 0, 1000 };
	struct timespec req = { 0, 1000000 };
	fd_set rset;
	int i, n=1;
	double result;

	FD_ZERO(&rset);
	FD_SET(0, &rset); //stdin

	if(argc > 1) {
		timeout.tv_usec = atoi(argv[1]);
		req.tv_nsec = timeout.tv_usec * 1000;
	}

	if(argc > 2)
		n = atoi(argv[2]);

	printf("timeout:\t%8d usec\nloops:\t\t%8d\n\n", timeout.tv_usec, n);

	printf("gettimeofday:\t");
	fflush(stdout);
	for(result=0,i=0;i<n;++i) {
		if(gettimeofday(&timer1, NULL)) _exit(-1);
		if(gettimeofday(&timer2, NULL)) _exit(-1);

		result +=
(timer2.tv_sec*1000000+timer2.tv_usec)-(timer1.tv_sec*1000000+timer1.tv_usec);
	}

	printf("%6.1f usec\n", result/n);

	printf("nanosleep:\t");
	fflush(stdout);
	for(result=0,i=0;i<n;++i) {
		if(gettimeofday(&timer1, NULL)) _exit(-1);
		nanosleep(&req, NULL);
		if(gettimeofday(&timer2, NULL)) _exit(-1);

		result +=
(timer2.tv_sec*1000000+timer2.tv_usec)-(timer1.tv_sec*1000000+timer1.tv_usec);
	}

	printf("%6.1f usec\n", result/n);

	printf("usleep:\t\t");
	fflush(stdout);
	for(result=0,i=0;i<n;++i) {
		if(gettimeofday(&timer1, NULL)) _exit(-1);
		usleep(timeout.tv_usec);
		if(gettimeofday(&timer2, NULL)) _exit(-1);

		result +=
(timer2.tv_sec*1000000+timer2.tv_usec)-(timer1.tv_sec*1000000+timer1.tv_usec);
	}

	printf("%6.1f usec\n", result/n);

	printf("select:\t\t");
	fflush(stdout);
	for(result=0,i=0;i<n;++i) {
		if(gettimeofday(&timer1, NULL)) _exit(-1);
		if(select(1, &rset, NULL, NULL, &timeout) == -1) _exit(-1);
		if(gettimeofday(&timer2, NULL)) _exit(-1);

		result +=
(timer2.tv_sec*1000000+timer2.tv_usec)-(timer1.tv_sec*1000000+timer1.tv_usec);
	}

	printf("%6.1f usec\n", result/n);

	return 0;
}

--------------------------------------------------