Samsung Interview Question
Software Engineer / DevelopersCountry: -
Interview Type: In-Person
C++ follows a pay-as-you-go philosophy. In other words, unless you opt to use the more advanced features of the language, the code isn't any slower than if you had written C. That is, you could write a C program that would also be a valid C++ program and the two would be equally fast.
I liked that "pay-as-you-go."
But it breaks my heart when you said 'ANY' ... C++ has to be slower than C, though negligibly for the reasons stated.
C++ doesn't have to be slower; that's my whole point. C++ is only slower when you use certain C++ features. Stick to only the C features and it'll be just as fast.
C++ does not have dynamic polymorphism when you don't use it. Generally, functions are, in fact, bound at compile time -- only functions marked virtual have dynamic polymorphism. Otherwise, C++ wouldn't be a pay-as-you-go language.
Yes, dynamic polymorphism sometimes allows us to write more reuseable code at the cost of some performance, but that is a tradeoff that you choose to make on a per-function basis in C++. You ask yourself whether you need the behavior for each function. You could decide to not use it at all, if the problem you're trying to solve doesn't need it.
Pay-A-You-Go What does it means?
Do you want to say I will not use c++ features so my code will run equally fast. If you dont use c++ features, what do you compare your C code with. There is no point of comparison of C and C++ till you use the c++ features.
There is no point of comparing c and c++ on basic of compiling same code in '.c' and '.cpp' files as mentioned by you as 'functions'.
If you '.c' and '.cpp' extension is not responsible for performance, It is senseless and illogical!!
You can take C code and call it C++, and then there won't be a performance hit. So C++ can be as fast as C, if it's C-compatible C++ code.
If you use C++-specific features, your code may be slower, but that depends. If you used those features because you really needed them and you end up doing some crazy workaround in the C version, you may essentially be writing an ad-hoc, poorly-documented, bug-ridden, slow implementation of C++. Then C++ might be faster because the C++ compiler may have better optimizations than your ad-hoc implementation.
Another important point is that there are plenty of C++-only features that are not necessarily any slower than their C counterparts. For example, templates allow a great deal of code re-use without any performance drawbacks (since a different version will be made for each type at compile time). As another example, operator overloading is no slower than using functions to achieve the same effects in C -- internally, the operators are converted to function calls anyway.
eugene, I was happy to see pay-as-you-go used by you. Thats the whole point started from your post and now you say equally fast. C++ is based on a design pattern of Object Orient ism. Now thats the whole point of getting slower code. If you say just take a .c file and rename it to .cpp and it will run equally fast. Your ideology of c++ itself is wrong then. C++ means object oriented approach and reusability. as soon as reusability comes into picture, virtual usually start coming with it and ultimately little bit slower code.
You cant just write a "HelloWorld" program and say that -
hey both C and C++ are equally fast. I am not going to reply any more posts as its simple and not worth explaining.
@abhityagi85: I don't think we disagree on any technical subject matter here. If we disagree, it's only on whether a C program that passes C++ syntax checks (and many C programs do) can properly be considered a C++ program. I say yes because it complies with the syntax rules of the C++ language. Even if you think that such a program would constitute bad C++ style, a bad C++ program is still a C++ program. So for such a program, it could be as fast in C++ as in C.
You're saying that C++ is about object-orientation, and so in practice, C++ programs will leverage this and have the potential to be slower. I don't disagree with that.
A C program should be faster than C++ program.
- abhityagi85 October 18, 2011For Example, let us take an example of polymorphism. The C++ has dynamic or runtime polymorphism. So the function call on an object will result in v-table v-function etc etc which will certainly take time. However in C, we have static polymorphism or function overloading in which the flow control is decided at the compile time thus saving the additional steps at runtime.
C++ however makes possible to write better code, reusable but this comes at a little price.