From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: Using Pointers ? Date: Tue, 04 Feb 1997 19:45:06 -0800 Organization: Two pounds of chaos and a pinch of salt Lines: 46 Message-ID: <32F80242.71F2@cs.com> References: <32F662F9 DOT 1F3B AT post DOT comstar DOT ru> <32F66FB9 DOT 1FC2 AT cs DOT com> <32F67138 DOT D72 AT cs DOT com> <32f6cfe0 DOT 53398230 AT news DOT ox DOT ac DOT uk> Reply-To: fighteer AT cs DOT com NNTP-Posting-Host: ppp204.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp George Foot wrote: > > Is this true? I was under the impression that gcc's god-like > optimisation would generally make better choices than the average > coder for register variables, i.e. it would figure out that these > ought to be registers without being told for itself. Just a rumour, > though... > > ...which brings up another point: won't the first two functions also > compile to this? i.e. won't gcc either (a) remember the memory > locations it's using or (b) temporarily registerise them without being > told? The reason the third function is likely to be faster is because it reduces to a series of integer operations, instead of a pointer dereference and an integer operation for each iteration. Of course, you're right that it depends in large part on what optimizations are being used. If you let gcc do its thing, it will most likely put 'fld1' and 'fld2' in registers. It _may_ even, with maximum optimizations, unroll the loop into: fld1 += 100000; fld2 -= 100000; In this case, the last function would take almost no time no matter how large the for loop. Because the first and second involve pointer dereferences, it's hard to say whether they could so easily be optimized in this fashion. Maybe so, but to know for sure you have to look at the assembly output. :) BTW, under ANSI C, classifying a variable declaration as 'register' is not guaranteed to have any additional effect. What you are doing is essentially telling the compiler, "I'd like it if you would put this in a register." The compiler is still free to do so or not as it pleases. I would, however, be very curious to see the code of the third function compiled both with and without the 'register' storage class at the same level of optimization. I'd do it myself but I don't have enough time tonight. -- --------------------------------------------------------------------- | John M. Aldrich, aka Fighteer I | fighteer AT cs DOT com | | Proud owner of what might one | http://www.cs.com/fighteer | | day be a spectacular MUD... | Plan: To make Bill Gates suffer | ---------------------------------------------------------------------