C vs C ++性能. [英] C vs C++ performance.

查看:130
本文介绍了C vs C ++性能.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google上搜索了"C vs C ++".看起来C和C ++之间没有相同的性能差异.但是我认为这是完全错误的,因为大多数系统软件都是使用C编程语言或汇编语言编写的,而不是使用C ++或任何其他语言编写的.那么C比C ++快吗?

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++ ?

推荐答案

由于这种方法,这个问题是完全不正确的:通常没有性能".只能考虑某些特定代码的性能.以及特定的编译器实现.和平台.

然后,只能对两种语言都有效的代码进行比较本身.
因为您总是可以用C ++编写一些无效的C代码(这很有可能,从这个意义上讲,C不是C ++的确切子集),还有一些无效的C代码像C一样.在这种情况下,性能只是没有意义.

最后,您可以使用不同的C和C ++代码对某种算法进行编程.但是在这种情况下,它确实取决于以上所有内容,还取决于实现的质量.因此,这种比较方式也没有明确定义.

—SA
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


C ++打击了虚函数.但这不是该语言的缺点,因为如果您使用C中的函数指针来模拟虚拟函数,则会获得类似的性能提升.

C ++具有许多功能,可以非常快速地编写非常复杂的代码.例如,模板.在许多情况下,由于编译器对数据类型的了解更多,因此C ++的速度也可能更快,从而降低了指针混叠的可能性.

除此之外,这两种语言之间的性能差异很小.

来自维基百科:

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.



这些别名优化可能会导致c ++代码的性能大幅提高.为了在c中获得相同的性能,需要明智地使用strict关键字.

-Debdatta Basu.



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.


到目前为止,尚未解决的一个方面是,它是否重要.

在现代系统中,很少有语言是决定性能的重要因素.

结构代码的方式比编译器产生更大的影响.

仅仅避免效率低下的编程实践,可以比担心语言更多的收获.

在过去,当12MHz的处理器速度较快时,所使用的语言确实很重要.对于那些试图提高性能,编译为汇编程序,只是计算我们的代码正在生成的指令数量和指令类型,以至于在这里和那里缩短几个周期的人们来说,这是一种常见的做法.

这些天,我不用理会.我写的大部分内容都几乎没有对CPU使用率产生任何影响.

如果避免两个常见的陷阱,您会走得更远...
1.尽量不要按值传递对象.如果可以,请使用const引用.
2.如果不需要,不要在内部循环中实例化对象.
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.


这篇关于C vs C ++性能.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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