From owner-freebsd-arch Wed May 16 1:37:20 2001 Delivered-To: freebsd-arch@freebsd.org Received: from mailman.zeta.org.au (mailman.zeta.org.au [203.26.10.16]) by hub.freebsd.org (Postfix) with ESMTP id 459CD37B422; Wed, 16 May 2001 01:37:09 -0700 (PDT) (envelope-from bde@zeta.org.au) Received: from bde.zeta.org.au (bde.zeta.org.au [203.2.228.102]) by mailman.zeta.org.au (8.9.3/8.8.7) with ESMTP id SAA26931; Wed, 16 May 2001 18:37:00 +1000 Date: Wed, 16 May 2001 18:35:42 +1000 (EST) From: Bruce Evans X-Sender: bde@besplex.bde.org To: Matt Dillon Cc: Terry Lambert , dave , freebsd-questions@FreeBSD.ORG, arch@FreeBSD.ORG Subject: Re: Gettimeofday Again... In-Reply-To: <200105151737.f4FHbBL55271@earth.backplane.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-arch@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 #include #include #include 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 #include #include #include 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