Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I've been recently checking about benchmark tests for the performance of programming languages but couldn't find a good answer. I've seen that at times, C++ will perform better than plain C. The thing is, apart from saying that, people are not providing examples(like "oh, on a binary tree with size X it will be better because....") or at least a more technical explanation.

Can anyone help?
Posted

There is no just C and just C++. There are different compilers for different platforms. And with every compiler, it's possible to write anything in such a stupid way killing performance quite well. Having all that, how are you going to compare languages? And if you also remember that you can actually write in C using C++ compiler...

No wonder "people" (those you refere to) "are not providing examples". The statements they made are hardly based on something real. Languages really can be different in performance, but the attempts to compare are usually simply invalid. For comparison, you need to have identical code samples. But if the code is really identical, this means the same language, such as in case of C disguised as C++.
A language that doesn't affect the way you think about programming, is not worth knowing.

—SA
 
Share this answer
 
Comments
CPallini 19-Feb-14 12:36pm    
My 5.
Sergey Alexandrovich Kryukov 19-Feb-14 13:26pm    
Thank you, Carlo.
—SA
Virtual functions could be faster than a lot of switch/case.

On the other hand, often the performance dépends more on the algorithm than the actual language. And with librairies like STL, it is often easier to write efficient algorithm.

For example, if you have a bad algorithm written in C++ and you have the choice between rewritting the algorithm for a better one or convert code to C but keep same algorithm, you might get an improvement of an order of magnitude in the first case while it be just noticeable in the second case...

Thus given that development time is not infinite, you might be able to create a more performant or complete application in the same amount of time using a more high level language.

Theorically one could wrte the most performant program in assembler... but adding the time constraint into the equation, often it won't make any sense.

Another case where C++ code might be more performant is when libraries that are used are better than the code you would be able to write in a reasonable amount of time...

And by the virtue of templates, C++ is able to do a lot of optimizations at compile time...
 
Share this answer
 
v2
Comments
Stefan_Lang 20-Feb-14 3:23am    
<quote>"Thus given that development time is not infinite, you might be able to create a more performant or complete application in the same amount of time using a more evoluated language."
Not sure what "evoluated" means, but I think you hit the central point right there! ;-)
Philippe Mori 20-Feb-14 8:44am    
High level...
Paulo Augusto Kunzel 20-Feb-14 6:56am    
Great answer, you have my 5.
It depends.

For smaller projects, it's generally best to use whatever language the programmers feel most comfortable with, and only check on performance if you realize there is an actual problem. You can mix and match C with C++, so if you believe you can get better performance switching from one to the other for just one source code module, or even one function, you can.

For larger projects (measured in years of development rather than weeks or months), C++ is usually better, because it gives you better tools for structuring your application. This also means that it's often easier to pinpoint the performance bottlenecks, and thus to improve performance.

There is one catch of C++ however that you need to be vigilant about: virtual functions and polymorphism come at a cost that can not be programmed away after the fact! If you design a C++ class that you know will be allocated a lot at runtime, to the point where it may be relevant to performance, that class should better not have virtual functions! If they have, construction and destruction will take significantly longer! In one particular case I've seen an application slow down by up to 20% just because I replaced a basic struct and a couple of related functions with a class hierarchy: it wasn't the reimplementation of the functions that caused the bottleneck, but the additional time needed to set up and clean the virtual function tables!

That said, C++ can also occasionally be faster than C, even if it's the same programmer at work: the compilers are exceptionally good at optimizing code, but the C++ compiler has an easier job at this, because it has lot more information to work with! In fact the compilers are so good that programmers are advised to not manually perform trivial (or even not so trivial) optimizations: doing so might result in obscuring relevant information that the compiler would need to find an even better optimization!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Feb-14 11:30am    
Some good points here, a 5.
I would focus on the fact that the whole idea of performance comparison is not quite valid; please see Solution 3.
Any absolute "short statement" on performance just should not be taken seriously.
—SA
CPallini 19-Feb-14 12:40pm    
My 5. I don't completely agree, however I recognize it is a good answer.
After reading some other solutions and the comments on my own, I feel inclined to provide yet another, somewhat different solutionstatement:

1. Don't bother with the question of performance until you've
a) actually written a program,
b) ran it,
c) detected an actual performance problem, and
d) found the cause of the performance problem.

2. Fix it by whatever means are most appropriate.

In doing so, you'll save a lot of time on premature performance coniderations that in the end don't actually matter anyway, and have a lot more time on your hands to focus on the relevant parts of your optimization.

When it comes to choosing a language, performance shouldn't be the deciding factor. There are always other aspects that are much more important in this decision.
 
Share this answer
 
Comments
Philippe Mori 20-Feb-14 8:48am    
True but they are some optimizations that should be done anyway... like trying to minimize number of SQL request or avoiding algorithm that are O(n²) or worst if possible.
Stefan_Lang 20-Feb-14 9:37am    
If the algorithm or SQL request ends up only being called on relatively small data sets, or so rarely that it takes up less than 0.1% of the total processing in your application, then it still may not be worth the effort.

Of course, during implementation you shouldn't go out of your way to make your function implementations inefficient. What I'm saying is that you shouldn't lose sleep over your function not being optimally performant, until you know for a fact that it's a bottleneck of your application.
Well written C++ shouldn't perform much worse than well written C. That's all we can say, in my opinion.
 
Share this answer
 
v2
Comments
Albert Holguin 19-Feb-14 10:09am    
I would agree with CPallini on this topic. It's all about who writes the code and knowing what are the things that can cause slowdowns in code.
Paulo Augusto Kunzel 19-Feb-14 10:34am    
That it true, but what I would like to know if this question is something like: Given the same program, algorithm, etc... When does one implementation might get an edge over the other
Albert Holguin 19-Feb-14 10:40am    
The thing is... you can make them equally fast if you avoid the pitfalls. For example, some of the std containers pre-allocate data because dynamically allocating data is slow. So even if you only load one object into the container, it will still take up (let's say) the size of five objects just in case you load more. If you exceed the container size, then it has to reallocate memory and copy the old data into the larger allocation. Well, if you know your size, you can always preallocate the memory and save yourself a lot of time, this goes for both C++ and C. So, it's more like knowing what causes slowdown in each and avoiding the situations.
CPallini 19-Feb-14 11:15am    
C++ was not born to outperform C. In fact it can't. For small tasks you have to carefully use C++ to obtain the same performance of a good C implementation.
Sergey Alexandrovich Kryukov 19-Feb-14 11:29am    
My 5. I added some works to it, please see Solution 3.
—SA

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