C vs C++数字食谱

发布于 2024-10-22 02:23:11 字数 160 浏览 2 评论 0原文

我开始阅读“C 中的数值食谱”这本书...我尝试使我的程序更高效、更快...那么,使用 C 或 C++ 是同一件事吗?我知道 C++ 是 C 的超集...但是,“cmath”库(c++)与“math.h”库(c)之间有区别吗?它会影响执行速度?...为了...我可以使用 C++ 与 C 程序没有任何区别?

I started to read the book "numerical recipes in C"... I try make my programs more efficients and faster... So, is the same thing, to use C or C++? I know that C++ is a super-set of C... But, are there differences between "cmath" library (c++) with "math.h" library(c)? It will intervene in the velocity of execution?... in order to that... i can use C++ without any difference with a C program?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

优雅的叶子 2024-10-29 02:23:11

首先,C++不是C 的超集。虽然有很多相似之处,但也有差异,而且两者都不是对方的子集。

既然我们已经解决了这个问题,Numerical Recipes 使用了一个特别简单的 C 子集,并且您在 C++ 程序中使用代码时应该不会遇到很多困难。大多数算法无需修改即可工作[1]。

您不必担心像 这样的细节;在大多数平台上,它只是 C 数学标头上的一个薄垫片,并且不会引入任何开销。一般来说,C++ 也是如此:当您编写看起来像 C 代码的代码时,几乎不会引入任何额外开销。

[1] 他们在 C 语言中工作; 《数值食谱》是一本有用的参考资料,但其中包含的程序并非完全没有错误,也不是所有算法都是最先进的。有些算法在数值上是糟糕的选择。如果您对所讨论的任何主题非常感兴趣,请务必阅读参考资料,然后查找有关该主题的更多最新材料。

First off, C++ is not a superset of C. Though there are many similarities, there are also differences, and neither is a subset of the other.

Now that we have that out of the way, Numerical Recipes uses an especially simple subset of C, and you should not encounter many difficulties in using the code in a C++ program. Most of the algorithms will Just Work without modification[1].

You shouldn't worry about details like <cmath>; on most platforms, it's just a thin shim over the C math headers, and does not introduce any overhead. Broadly, this is true of C++ in general: when you're writing code that looks like C code, little or no additional overhead is introduced.

[1] In as much as they work in C; Numerical Recipes is a useful reference, but the programs contained therein are not entirely bug-free, nor are all of the algorithms state of the art. Some of the algorithms are numerically poor choices. If you become seriously interested in any of the topics discussed, be sure to read the references, and then to look for more current material on the subject.

守望孤独 2024-10-29 02:23:11

C++ 有时会因效率较低和/或比普通 C 更臃肿而受到批评。当然,有更多方法可以编写低效代码 - 例如,您应该避免在紧密的内部循环中调用虚拟函数。

话虽如此,对于基本的数字代码,我预计 C 和 C++ 之间不会有很大差异。一旦您开始研究科学应用程序,您就可以从 C++ 模板元编程中获得很多好处,从而提高效率(同时,代码更可读,尽管编译错误有点棘手)。一个很好的例子是 Blitz++ 库——它明确设计用于竞争低级代码(在本例为 Fortran)。

编辑:删除了子集/超集相关的声明。

C++ sometimes gets a bad rep for being less efficient and/or more bloated than plain C. Certainly there are more ways to write inefficient code -- you should avoid having virtual function calls in tight inner loops, for example.

With that said, for basic numerics code, I would not expect a great deal of difference between C and C++. Once you start looking at scientific applications, you can get a lot of mileage out of C++ template metaprogramming for better efficiency (and, at the same time, more readable code, although compilation errors are a bit gnarly). A good example of this is the Blitz++ library -- it's explicitly designed to compete low-level code (in this case Fortran).

Edit: Removed subset/superset related claims.

千鲤 2024-10-29 02:23:11

应该是完全一样的。如果您想了解可以从 C++ 中获得什么数值,您可以查看模板元编程数值算法——当您在编译时了解足够的信息以在构建期间进行一些计算时,它们就会起作用。

Should be exactly the same. If you want to see what you can get from C++ numerically, you can look at template meta-programming numerical algorithms -- they work when you know enough information at compile time to do some of the computing during build.

翻身的咸鱼 2024-10-29 02:23:11

当然,这取决于您的特定编译器等,但我的观点是您应该使用 C++,并且它的执行速度至少应该与 C 一样快或更快(由于更现代的优化)。

当然,C++ 提供了更多功能齐全,这就是我推荐它的原因。如果您使用该功能,可能会影响性能。但 C++ 仍然非常快(比当今使用的大多数语言都快)。如果您不需要这些高级功能,请不要使用它们。

Of course, it depends on your specific compiler, etc. but my view is that you should use C++, and that it should perform at least as fast or faster (due to more modern optimizations) than C.

Of course, C++ provides a more functionality, which is why I recommend it. And if you use that functionality, that may possibly impact performance. But C++ is still very fast (faster than most languages in use today). And if you don't need those advanced features, don't use them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文