C 明显比 C++ 快吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6955114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is C notably faster than C++
提问by Will03uk
As far as I understand all scripting languages and core scientific programs are usually written in C; this renders the implementation messy yet in a way straight to the point.
据我所知,所有脚本语言和核心科学程序通常都是用 C 编写的;这使实现变得混乱,但又直截了当。
I understand these people would like to max their performance but is there a real difference between using C strings and C structures to using C++ classes; C++ seems to work the same way, apart from virtual functions, it stores a class function once and every instance of that class calls that one function.
我理解这些人想要最大化他们的性能,但是使用 C 字符串和 C 结构与使用 C++ 类之间有真正的区别吗?C++ 似乎以相同的方式工作,除了虚函数之外,它存储一个类函数一次,并且该类的每个实例都调用该函数。
What makes C faster and is it a notable difference in a project such as python or sqlite who have to be the fastest?
是什么让 C 更快,它在诸如 python 或 sqlite 之类的必须是最快的项目中是否有显着差异?
采纳答案by Potatoswatter
C++ is often used for scientific programs. The popularity of C may be waning in that domain. Fortran remains popular as a "low-level" language.
C++ 常用于科学程序。C 在该领域的流行度可能正在减弱。Fortran 仍然是流行的“低级”语言。
In C++, "you only pay for what you use." So there is nothing that would make it any slower than C. In particular for scientific programs, expression templates make it possible to perform some custom optimization using the template engine to process program semantics.
在 C++ 中,“您只需为使用的东西付费”。因此,没有什么比 C 更慢的了。特别是对于科学程序,表达式模板可以使用模板引擎来执行一些自定义优化来处理程序语义。
The reason C is chosen for projects such as Python is that many people understand it (relatively) fully, so a large codebase will not confuse many of a large pool of contributors.
为 Python 等项目选择 C 的原因是很多人(相对)完全理解它,因此大型代码库不会混淆大量贡献者中的许多人。
SQLite has a requirement for small executable code size, where C does have a slight edge. Judicious use of C++ still allows use in embedded applications, but it is less popular due to fear that unwanted language features will creep in.
SQLite 需要较小的可执行代码大小,而 C 确实有一点优势。明智地使用 C++ 仍然允许在嵌入式应用程序中使用,但由于担心会出现不需要的语言功能,因此它不太受欢迎。
回答by David Rodríguez - dribeas
I don't think that the reason is so much related to performance as it is to interoperability. The C++ language is more complex than the C language, but from a performance point of view there shouldn't be a notable difference in either way. Some C++ constructs are faster than the C equivalent (std::sort
is faster than qsort
) and there are probably good examples of the other way around.
我认为原因与其说是与互操作性有关,不如说是与性能有关。C++ 语言比 C 语言更复杂,但从性能的角度来看,这两种语言应该没有显着差异。一些 C++ 构造比 C 等价物std::sort
更快(比 快qsort
),并且可能有相反的很好的例子。
EDIT:On the interoperability side...
编辑:在互操作性方面......
Basically, the C++ standard does not define some of the things that might be needed for easy interoperability between binaries created with different compilers/versions. The most notable issue here would be the naming convention for the symbols in the binary. In C, the language defines a single mapping from each symbol in code to the binary symbol name. A function called my_function
will create a symbol in the binary called my_function
. On the other hand, and due to features like function overloading, the names of C++ functions have to be mangled(translated into different function symbols in the binary, encoding the types of the arguments and return types), and the standard does not define how the mangling is performed. That in turn means that the same function in C++ can be compiled to different symbols depending on the compiler (unless extern "C"
is used to force C interoperability for those functions in C++).
基本上,C++ 标准没有定义一些可能需要的东西,以便在使用不同编译器/版本创建的二进制文件之间轻松互操作。这里最值得注意的问题是二进制文件中符号的命名约定。在 C 中,该语言定义了从代码中的每个符号到二进制符号名称的单个映射。调用的函数my_function
将在名为 的二进制文件中创建一个符号my_function
。另一方面,由于函数重载等特性,C++ 函数的名称必须被修改(在二进制中翻译成不同的函数符号,对参数的类型和返回类型进行编码),并且标准没有定义如何执行 mangling。这反过来意味着 C++ 中的相同函数可以根据编译器编译为不同的符号(除非extern "C"
用于强制 C++ 中这些函数的 C 互操作性)。
At the end of the day, the interface between the scripting language and the native code would have to be a C interface anyway, even if the details of how it is implemented internally could be C/C++/any other native language.
归根结底,脚本语言和本机代码之间的接口无论如何都必须是 C 接口,即使内部实现的细节可能是 C/C++/任何其他本机语言。
(I am intentionally not wanting to enter into a flame war of language prefences, C++ is really powerful, but it is also a bit scary as it is a much more complex language than C, and some things that looksimple might have an impact on performance)
(我是故意不想进入语言偏好的War,C++真的很强大,但它也有点可怕,因为它是一种比C复杂得多的语言,一些看起来简单的东西可能会对表现)
回答by fortran
Languages are not inherently faster or slower, interpreters and compilers might be more or less efficient.
语言本身并不快或慢,解释器和编译器的效率可能更高或更低。
Besides that, higher level languages provide abstraction layers that usually have a runtime cost. If you are not using them, the compiler might be smart enough to strip them out, but that might not be possible if the semantics of the language do not allow to do it safely... And if you need them, implementing them by yourself in a lower level language will be probably slower than using the "slow" language.
除此之外,高级语言提供通常具有运行时成本的抽象层。如果您不使用它们,编译器可能足够聪明,可以将它们剥离出来,但是如果语言的语义不允许安全地这样做,那可能是不可能的……如果您需要它们,请自己实现它们使用较低级别的语言可能比使用“慢”语言慢。
回答by outmind
As Bjarne mentioned in [D&E] the effectiveness is one of the main goals of C++. So C++ is slower only when programmer uses its "extra" functions like virtual functions you mentioned, rtt information etc
正如 Bjarne 在 [D&E] 中提到的,有效性是 C++ 的主要目标之一。因此,只有当程序员使用其“额外”功能(如您提到的虚函数、rtt 信息等)时,C++ 才会变慢
So I think it is more of psychological reasons - C is used as it doesn't allow "slow" C++ features.
所以我认为这更多是出于心理原因——使用 C 是因为它不允许“慢”的 C++ 特性。