Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 16 May 2001 18:35:42 +1000 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        Matt Dillon <dillon@earth.backplane.com>
Cc:        Terry Lambert <tlambert2@mindspring.com>, dave <dleimbac@earthlink.net>, freebsd-questions@FreeBSD.ORG, arch@FreeBSD.ORG
Subject:   Re: Gettimeofday Again...
Message-ID:  <Pine.BSF.4.21.0105161759410.8173-100000@besplex.bde.org>
In-Reply-To: <200105151737.f4FHbBL55271@earth.backplane.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 15 May 2001, Matt Dillon wrote:

>     Also, using gettimeofday() is a ridiculous way to measure fine grained
>     time billions of times in production code.  I mean, sure, it works...
>     but getitimer() is about 5 times faster.

This seems unlikely, since most of the overhead for both is in the syscall.
Actual testing shows that getitimer() is a whole 10% faster on a Celeron
5.5 * 95 MHz:

$ sysctl kern.timecounter.hardware
kern.timecounter.hardware: TSC
$ sysctl machdep.tsc_freq
machdep.tsc_freq: 522493830
$ cat z.c
#include <sys/types.h>
#include <sys/time.h>

#include <err.h>
#include <stdlib.h>

int
main(void)
{
	struct timeval tv;
	int i;

	for (i = 0; i < 1000000; i++)
		if (gettimeofday(&tv, NULL) != 0)
			err(1, "gettimeofday");
	exit(0);
}
$ cc -O -o z z.c
$ time ./z
        2.04 real         0.47 user         1.57 sys
$ cat q.c
#include <sys/types.h>
#include <sys/time.h>

#include <err.h>
#include <stdlib.h>

int
main(void)
{
	struct itimerval it;
	int i;

	it.it_interval.tv_sec = 0;
	it.it_interval.tv_usec = 0;
	it.it_value.tv_sec = 100;
	it.it_value.tv_usec = 0;
	if (setitimer(ITIMER_REAL, &it, NULL) != 0)
		err(1, "setitimer");
	for (i = 0; i < 1000000; i++)
		if (getitimer(ITIMER_REAL, &it) != 0)
			err(1, "getitimer");
	exit(0);
}
$ cc -O -o q q.c
$ time ./q
        1.84 real         0.46 user         1.37 sys

This is for my version of -current, which has a few pessimizations in
microtime().  Plain -current would be a few nsec faster.

OTOH, gettimeofday() may be significantly slower than getitimer() if the
hardware timecounter is slow:

# sysctl -w kern.timecounter.hardware=i8254
$ sysctl kern.timecounter.hardware
kern.timecounter.hardware: i8254
$ time ./z
        5.06 real         0.44 user         4.60 sys

Bruce


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0105161759410.8173-100000>