I discussed with several people about the performance between C and C++.
Most of the people mean that C is more high-performance than C++. I made
some research but I never found an academic resource with this topic.
C++ has polymorphism which uses vtables to point to the functions, this overhead to get the function can be slower than simply calling a known function. From what I know of C there is no polymorphism. Another feature that C lacks is exceptions which are also considered slow, really it depends on how they are being used I find. Those are the only two things that come to mind that are different in C and C++. Since they both compile into assembly it depends really on the techniques that are being used, someone could implement a vtable of their own in C which might be less efficient than if they just used C++.
It isn't really a matter of which is more efficient but a case of which is more efficient for the job at hand, although it might be more efficient to write it one case it could be drastically easier to write it another way (in terms of recursion) with the power of cpu's and gpu's these days unless you are writing operating system level code that runs continues in the background I don't really see much performance gain for potentially more work. In the case of java, it's become so popular yet it's terribly inefficient, but it's "safe" and easier to program in.
Standard C only uses simple pointers, there is no way to use reference( as far as I know)
So how do you pass huge objects( e.g. of type struct) into a function using pointer ?
C++ has it powers and advantages over C, but not by default you should do some thing to have the advanatges. You can code programms with C++ that has the Fortran performance!!! with C it is impossible , how would one do Meta-Programming in C ?
struct mat{
mat(int m, int n ){
data = (double**)malloc(m* sizeof(double*));
for(i=0; i < m ; i++)
*(data+i) = (double *)malloc(n * sizeof(double) );
}
//define some other things here
private:
double** data;
};
// your main(){}
void main(){
//do some tasks
mat a(10000, 10000 ), b(10000, 10000), c(10000, 10000) , d(10000, 10000);
c = add(a, b); // how to pass a and b , and how to assign the result to c
// may be you have a functtion add( a, b, c) ; but how if I how 10 objects to be added, mulitplied and so on
d = multi( add(a,c) , b ) ;// and now do in c
// one can make it more complicated
}
in C++ I would do all the job by using the implementation of expression template :-)
I think you have to look at the larger picture. First of all the hardware out today is so overpowered compared to what software needs that we have languages consistantly in the top 10 that require frameworks preloaded into memory and actually wait until run time to compile, if that doesn't bog your program down then a couple extra jumps to and from a vtable aren't going to either. The human brain can only precieve and react to things so fast, I'd honestly argue that on a modern system it's impossible to see this "Higher Performance" that people are always saying C has over C++. Even if you are targeting slower hardware like thin clients for example, it won't matter that your program executes 1.2 seconds faster then mine if it takes you ten times longer to write and debug it. You can't even overload functions in C, how the hell do you expect to write modular code?
EDIT: I wrote my post before I actually read what xerzi said. Mine is basically the same thing written a little more aggressivley.
I didn't run the C and first C++ versions, but the exe size was 7.5 for the C and 17.5 for the C++. It's not always about the code, sometimes it's still about size and performance.
As a caveat, I can't get the last version, the unix based one, to work since I run Windows.
I'd be leery of wanting maintainability over and above performance and size in a high performance application, that may have to run in a shared environment.
I do like C++ though, for the overloading and OO aspect. Still, I tend to go more with what I know and I tend to minimize the number of objects and work more with built-in fundamental types as much as I can.
I do this by identifying things that are invariant or change little and/or infrequently. These become objects.
Most of the people mean that C is more high-performance than C++.
C++ is as efficient as C.
C++ has built in support for OO. Polymorphism can be implemented in C, but there is no language support for it. Where polymorphism is used in C, there is still a run-time decision that needs to be taken and so there will be a runtime impact just as in C++.
The human brain can only precieve and react to things so fast, I'd honestly argue that on a modern system it's impossible to see this "Higher Performance"
and keep it mind C is the father of C++ :-)
So if you try to compare them with each other, it is the same if one would try to compare a father born in 1960's with his child born in 1990's :-)