Obviously, I am interpreting this question as structured vs object oriented programming. I personally have used C++ for ages, without caring to make a class -- and i really appreciate the fact that it dosnt force a programming paradigm on us :)
Regarding optimization, one thing is clear -- especially in the GNU context: GCC does optimization on an intermediate form of code that it derives from the front-end language like C or C++. Hence, optimization must be just as good for both. In fact, I believe that taking the CFront route (C++ -> C -> ASM) instead of the GCC route (C++ -> ASM) will produce assembly that's just as good, but will take much longer to do so.
So, what the point? How is C++ optimization different? I'd say that the "optimization needs" of a C++ program are different.
Let's take an illustrative historical case of encapsulation -- encapsulation brought in a new era where the number of functions written by a programmer increased manifold! Firstly, because C++ dissuades the use of macros, and secondly, because there is a higher tendency of making constructors and destructors in C++, whereas in C, you'd type in the whole thing each and every time you needed it. Thus, older compilers who did not have good enough support for inlining functions, often failed to produce overall good C++ code.
Of course, any self-respecting compiler today has really good inlining support, so this example I have given probably no longer holds. So now, lets move on to simple polymorphism: virtual functions are implemented using a lookup table that gives you a function pointer for each derived class type. Thus, these kind of functions can simply not be inlined, and they also use "jump to address in a variable"; something like this: (*var)(). A programming style that enforces use of such control jumps is BAD. The reason is that most computer architectures today have built-in support for branch prediction and usage of such statements defeats their purpose. I do not deny that you can do this in C as well; but you would usually not! Whereas, the use of virtual functions in C++ is almost a norm!
I have no idea about multiple inheritence etc., god knows why they created such a feature! Also, I have never used STL, so I don't really know how the widespread use of STL influences the optimization needs of C++ code.
All said, I definitely agree that C++ is a wiser choice than C for any hardcore hosted developmental work, because of lesser development time. I just seriously recommend restricting polymorphism to only those places where it really really simplifies things.
BTW, G++ usually produces bloat in the form of a symbol table, that's used for debugging etc., it won't even be copied to memory... just sits on your harddisk.
No comments:
Post a Comment