什么时候C ++比C表现更好? [英] When does C++ perform better than C ?

查看:83
本文介绍了什么时候C ++比C表现更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我最近一直在检查有关编程语言性能的基准测试但是找不到一个好的答案。我已经看到有时候,C ++会比普通的C表现更好。事情是,除了说,人们不提供例子(比如哦,在尺寸为X的二叉树上,它会更好,因为... 。)或者至少是一个更技术性的解释。



任何人都可以帮忙吗?

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?

推荐答案

有不只是C而且只是C ++。不同的平台有不同的编译器。对于每个编译器,都可以以如此愚蠢的方式编写任何内容,从而很好地破坏性能。拥有这一切,您将如何比较语言?如果你还记得你可以使用C ++编译器实际用C语言编写...



难怪人(你引用的那些)没有提供例子。他们所做的陈述很难基于真实的东西。语言的性能确实不同,但比较的尝试通常是无效的。为了进行比较,您需要具有相同的代码示例。但如果代码真的相同,这意味着相同的语言,例如在C伪装成C ++的情况下。

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++.
Alan Perlis 写道:

一种不影响编程方式的语言,不值得了解。



-SA


虚拟功能可能比许多交换机/机箱更快。



另一方面,性能往往更多地取决于算法而非实际语言。对于像STL这样的库,通常更容易编写有效的算法。



例如,如果您有一个用C ++编写的错误算法,您可以选择重写算法以获得更好的算法或将代码转换为C但保留相同的算法,你可能会在第一种情况下得到一个数量级的改进,而在第二种情况下它是明显的...



因此,鉴于开发时间不是无限,您可以使用更高级别的语言在相同的时间内创建更高性能或完整的应用程序。



理论上可以提供最高性能的应用程序汇编程序中的程序...但是将时间约束添加到等式中,通常它没有任何意义。



C ++代码可能性能更高的另一种情况是当使用的库比你能够在合理的时间内编写的代码更好...



并且凭借模板,C ++是能够在comp上做很多优化ile time ...
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...


取决于。



对于较小的项目,通常最好使用程序员最喜欢的语言如果你意识到存在实际问题,那就很舒服,只检查性能。你可以将C与C ++混合搭配,所以如果你相信你只需要一个源代码模块,甚至一个函数就可以从一个到另一个转换到更好的性能,你可以。



对于较大的项目(以开发年数而不是数周或数月来衡量),C ++通常更好,因为它为您提供了更好的结构化应用程序的工具。这也意味着通常更容易查明性能瓶颈,从而提高性能。



有一个C ++的问题,但是你需要警惕:虚函数和多态性的代价是事后无法编程的!如果你设计一个C ++类,你知道它将在运行时被分配很多,到可能与性能相关的程度,那么该类最好不要有虚函数!如果他们有,建设和破坏将需要更长的时间!在一个特殊的情况下,我看到一个应用程序减速高达20%只是因为我用类层次结构替换了一个基本结构和几个相关函数:它不是导致瓶颈的函数的重新实现,而是设置和清理虚拟功能表所需的额外时间!



也就是说,C ++偶尔会比C更快,即使它是同一个程序员在工作:编译器非常擅长优化代码,但C ++编译器在这方面工作更容易,因为它有更多的信息可供使用!事实上,编译器非常好,建议程序员不要手动执行琐碎(甚至不是那么琐碎)的优化:这样做可能会导致模拟编译器需要找到更好的优化所需的相关信息!
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!


这篇关于什么时候C ++比C表现更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆