Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I searched "C vs C++" on Google. As it seems there is no identical performance difference among C and C++. But I think that is completely wrong, Because most of the system software are written using either C programming language or Assembly Language, not C++ or any other. So is C Faster than C++ ?
Posted

The question is totally incorrect due to the approach: there is no "performance" in general. The performance can be considered only in regard to some particular code. And particular implementation of a compiler. And platform.

Then, the comparison itself can only be done for some code which is valid in both language.
As you can always write some C code invalid in C++ (this is quite possible, and in this sense, C is not the exact subset of C++), as well as some C++ code invalid as C. In such cases, the question of the performance is just makes no sense.

Finally, you can program some algorithm using different C and C++ code. But in this case, it does depends on all of the above and also on the quality of implementation. So, this way of comparison is also not defined in some unambiguous way.

—SA
 
Share this answer
 
v2
Comments
nv3 22-Aug-12 16:41pm    
What you say is scientifically all correct, Sergey -- my 5 so far. But please take into consideration who asked and for what reason. Probably someone has tried to talk Pravinda into using C instead of C++, because it produced soooo much better code. And now he his probably uncertain of how to deal with that. So let's give him a simple and straight answer that he is able to digest:

Pravinda, don't worry a second about the performance difference of C and C++ and use C++ when there is a decent C++ compiler on your platform. Once you are an expert in C++, and you have written some hundred thousand lines of code in it, then you might start to worry about the performance differences of C and C++. But probably at that time you wouldn't, because you have learned in the meantime that performance problems are almost always related to bad software design and very very rarely arise out of the deficiencies of a language or compiler. And by that time you have appreciated the improvements of C++ so much that you wouldn't even think of going back to C.
Sergey Alexandrovich Kryukov 22-Aug-12 18:22pm    
I actually up-voted the answer by JackDingler who was the first to go in for real performance consideration. By doing so, from the very beginning, I admitted that the considerations themselves make sense, and they make practical sense, first of all, and really useful. I just say that to make them completely valid, they need a note putting principle limitation to such consideration, which is also important, and also practical.

Like in the case of the laws of natures: they are only valid when the limits of their applicability are shown.
--SA
pasztorpisti 24-Aug-12 20:35pm    
5ed, quite a philosophical approach :-)
Sergey Alexandrovich Kryukov 24-Aug-12 23:33pm    
Well, thank you.
--SA
C++ takes a hit on virtual functions. But it is not a disadvantage of the language, as you will get similar performance hits if you emulate virtual functions using function pointers in C.

C++ has a lot of features that can make very complex code very fast. For example, templates. C++ could also be much faster in a lot of cases as the compiler knows more about your data types, reducing the probability of pointer aliasing.

Besides this, there is very little performance difference between the languages.

From Wikipedia:

In C, any function pointer argument may alias any other function pointer argument. The compiler must assume that any accesses through these pointers can alias. This dramatically restricts the potential for optimization.

In C++, pointer arguments are assumed not to alias if they point to fundamentally different types. This allows more optimizations to be done than in C.


These aliasing optimizations may lead to big performance gains in c++ code. To get the same performance in c would require judicious use of the restrict keyword.

-Debdatta Basu.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Aug-12 18:30pm    
Those are useful and reasonable considerations, my 5, but one always needs to understand the limitation in validity of comparisons, which I describe in my answer -- please see.
--SA
Debdatta Basu 22-Aug-12 21:54pm    
Absolutely. Especially with c/c++, performance will be very close most of the time.

-DB
pasztorpisti 24-Aug-12 20:45pm    
+5 Virtual functions in C++ mean double dereferencing (picking up vtable pointer and then the function address) while the same in C is just one dereferencing. But the real performance hit is usually not the dereferencing, its rather screwing up the branch prediction of the processor that happens in both cases when you jump to indirect address. The other difference is exception handling for which you can turn off code generation in all major C++ compilers. C or C++? Definitely C++. Either way, both of these languages are still must know languages today (unfortunately).
Debdatta Basu 25-Aug-12 8:16am    
Well how would you emulate inheritance in c without creating some sort of vtable? have a copy of the vtable for each class instance as opposed to a pointer to it? That would increase the size of the instances... anywas I agree. with c or c++, id say definitely c++. Far better language overall.
One aspect not addressed so far, is whether it matters or not.

In modern systems, it's rare that the language is a serious factor in determining performance.

The way you structure your code, makes a bigger impact than the compiler.

Simply avoiding inefficient programming practices, can get you more mileage than worrying over the language.

In olden days, when 12mhz was a fast processor speed, the language used did matter. It was a common practice for those trying to squeeze a little more performance, to compile to assembly, just to count how many, and what kinds of instructions our code was producing, to shave off a few cycles here and there.

These days, I don't bother. Most of what I write, barely makes a mark on CPU utilization.

You'll go far if you avoid two common pitfalls...
1. Try not to pass objects by value. Use const references if you can.
2. Don't instantiate objects inside of inner loops if you don't have to.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Aug-12 18:26pm    
This is again an important point even though it formally goes beyond the question, but we are trying to really help, not just to answer questions, right?
And, by the way, nearly every time I managed to fix some performance, it was general code design flaw or bigger architectural flaw... a language is not often becomes a bottleneck even if the performance is distinctly different. (Well, some languages could be prohibitively bad, but we are not discussing extreme problems...)

I still think that my answer is correct because it puts limitations to the validity of performance comparison -- something which always should be understood -- please see my discussion with nv3.

My 5 again.
--SA
Debdatta Basu 22-Aug-12 21:50pm    
This is somewhat true for data intensive applications. But it's certainly not true for compute intensive ones. You will find 2x performance gain by simply porting from java to c++ and around 200x while porting from python to c++. The gains will be much lower in applications that are data limited.

Raytracers, Engineering simulators, Image processing, CAD etc some to mind.

-Debdatta Basu
JackDingler 23-Aug-12 2:04am    
Right, but that's become an edge case. I know that folks are still writing CPU intensive applications, but in the majority of business apps, this isn't the case. Word Processors used to eat up quite a bit of CPU time. Now how much time do they cut into? Spreadsheets? Common print jobs?

And in those graphics cases you'll gain even more performance going to C/C++ variants like OpenCl and CUDA.
Debdatta Basu 23-Aug-12 3:42am    
That's an edge case if GUI/application programming is all you're into. For spreadsheets, its ok if the number of entries are small. For large databases c++ is still king. It really depends on what you are writing. Simple to moderately complex GUI app? Managed languages all the way. Something a little more complex? consider c++.

CUDA/OpenCL are a niche application, and require careful coding to extract performance. They can not handle most programming paradigms, and their usefulness is limited in the general case.

However, your points are all valid, and so, my +5. :D

-DB
JackDingler 23-Aug-12 8:36am    
I did CAD programming for the CNC industry for six years.

I don't think that only looking at what I'm doing, represents a good sample size though.

I really doubt that a high percentage of programmers are writing CAD applications at this time. Surely not 80% or 90%. There just isn't the need for CAD programmers as much as there is for POS, mobile apps, etc...

I would be interested in seeing a current breakdown of programs by application type.
C is a subset of C++. That's why C++ has an increment operator built into the name. It's C with stuff added.

Are you trying to say that system software is never written in C++?
The current versions of the Windows OS's are largely written in C++.

Straight C is used for system software for a number of reasons.

1. The system was written before C++ was released.
2. The system has a C compiler ported to it, but not a C++ compiler.
3. The developer prefers to write in C.
4. Someone involved in the project, believes the misconception that C is faster than C++.
5. A design decision was made to stick to C, for architectural reasons.

Further, classes on their own don't slow down the execution time by a meaningful degree. The initial overhead in using objects, is that an object pointer (this), is passed to every member function behind the scenes. This adds an implicit push to the stack on every call.

In C, you would still be making that push when you pass an argument for a target object, unless you're just dealing with globals. But you can do that in C++ with static members too... So there's nothing gained on this account.

When it comes to virtual functions, C++ does see a small performance hit, because it does a table lookup for the correct function. You commonly see this in C though, when you're using lists, maps or arrays of function pointers. But if you have too much overhead in using virtual methods for a call, you'd optimize it out in C++, just like you would in C procedure that's eating up too many cycles.

The advantages of C++ outweigh the small disadvantages of performance.
namespaces and polymorphism eliminate some major headaches in C, which are issues with naming collisions.

If you prefer C, you can still program in C using C++, and gain the advantages of these features.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Aug-12 13:49pm    
C is not a subset of C++, but it is almost a subset. Even though most of your considerations are very correct and useful, good to understand, the only valid answer to the question is: the question is totally incorrect due to the approach: there is no "performance" in general. The performance can be considered only in regard to some particular code. And particular implementation of a compiler. And platform.

Then, the comparison itself can only be done for some code which is valid in both language.
As you can always write some C code invalid in C++ (this is quite possible, and in this sense, C is not the exact subset of C++), as well as some C++ code invalid as C. In such cases, the question of the performance is just makes no sense.

Finally, you can program some algorithm using different C and C++ code. But in this case, it does depends on all of the above and also on the quality of implementation. So, this way of comparison is also not defined in some unambiguous way.

Overall, I voted 4, just because some of your considerations are practically useful.

--SA
JackDingler 22-Aug-12 14:13pm    
Good points.

As a for instance, the K&R syntax isn't valid in C++

float Calc(Value1, Value2)
float Value1;
float Value2;
{
return Value1 * Value2;
}
Sergey Alexandrovich Kryukov 22-Aug-12 14:34pm    
Exactly! Thank you for this code sample, this is a good clarification.
--SA
Sergey Alexandrovich Kryukov 22-Aug-12 13:50pm    
After my note above, I decided to make another answer to clarify all that. Please see.
--SA
Espen Harlinn 22-Aug-12 15:28pm    
My 5 - I like the conclusion, but there is a lot more to modern C++ that gives the compiler opportunities to better optimize the generated code.
I think C++ is faster than C...may be i am wrong but it seems to me that.You can read a link here over this topic..
http://unthought.net/c++/c_vs_c++.html[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900