Subj : Re: OO compilers and efficiency To : comp.programming From : Flavius Vespasianus Date : Wed Jul 20 2005 02:18 am Brian wrote in news:dbj5f7$rdv$1@gondor.sdsu.edu: > So is this wrapping one stack frame on top of another? First the > subclasses method gets called then super's? Are compilers generally > smart enough to inline the two calls into one? What compiler implements polymorphism that way? > > Getter and setter functions are another simple example of what seems > to be waste. The method call works like this. The existing register > states are pushed onto the stack. The call is made. The getter > method returns a simple value on the stack. The call is returned. > The registers are popped. That must be about 20 instructions for > what might only take one, a simple memory read, or better yet, a > register read. Calling a non-virtual function is no slower than calling a C function. If the function is defined inline, compiler has no need for the call stack. And I can't imagine any compiler implementing a set function that could be implemented in one instruction having 20 push and pops. > > What happens if a getter or setter is right in the middle of a > tight loop? Let's say the loop has 200 instructions. All of a > sudden the loop is 10% less efficient. The alternative, in the absence of OO is a large case statement; hardly efficient either. > Anyway, those are some of the things that kind of nag at me. > I think it's a safe statement to say C can beat any OO compiled > program pound for pound given the same programmer skill and > adherence to language goals. I once reviewed a book manuscript for a publisher, the author of which would entirely agree with you -- people who think they can code faster than the compiler can optimize. The book consisted of all kinds of tricks in C to make your code faster. I benchmarked all of his examples and EVERY one of his optimization tricks resulted in slower code. > > Does everyone drop into C for critical code? Never. C is a horrible programming language has caused the software industry decades of suffering. .