From owner-freebsd-hackers Mon Jun 5 16:58:22 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from happy.checkpoint.com (happy.checkpoint.com [199.203.156.41]) by hub.freebsd.org (Postfix) with ESMTP id 26E7F37BE32 for ; Mon, 5 Jun 2000 16:58:16 -0700 (PDT) (envelope-from mellon@pobox.com) Received: (from mellon@localhost) by happy.checkpoint.com (8.9.3/8.9.3) id CAA40944; Tue, 6 Jun 2000 02:57:18 +0300 (IDT) (envelope-from mellon@pobox.com) Date: Tue, 6 Jun 2000 02:57:18 +0300 From: Anatoly Vorobey To: "Daniel C. Sobral" Cc: hackers@freebsd.org Subject: Re: Optimization Message-ID: <20000606025717.A40896@happy.checkpoint.com> References: <200006052347.IAA00583@daniel.sobral> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0.1i In-Reply-To: <200006052347.IAA00583@daniel.sobral>; from dcs@newsguy.com on Tue, Jun 06, 2000 at 08:47:42AM +0900 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Tue, Jun 06, 2000 at 08:47:42AM +0900, Daniel C. Sobral wrote: > Can someone discuss the performance trade-offs of the following two > alternative codes (and maybe suggest alternatives)? > > Problem: I need to retrieve two values from a table. > > Alternative A: > > x = table[i].x; > y = table[i].y; > > Alternative B: > > d = table[i]; > x = d & MASK; > y = d >> SHIFT; Alternative A should be much faster. The compiler should be smart enough to cache &(table[i]) in a register. OTOH I am not sure it'll be smart enough to cache a structure (d) in a register even though it might fit there. The first line of Alternative B should take roughly as much as the whole of Alternative A, and even more if the compiler is stupider (and sets up an array-copying operation to retrieve d instead of explicitly copying x and y). Of course, if table[i] includes anything else besides x and y, B is slower yet. You might want to declare x and y as register if that's alright with you. It might speed you up wrt next operations you do with x and y. -- Anatoly Vorobey, mellon@pobox.com http://pobox.com/~mellon/ "Angels can fly because they take themselves lightly" - G.K.Chesterton To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message