还是现在反过来了?

据我所知,c#在某些领域被证明比c++更快,但我从来没有勇气亲自测试它。

我想你们任何人都可以详细解释这些差异,或者告诉我有关信息的正确位置。


当前回答

毕竟,答案总要在某个地方,不是吗?:)

嗯,没有。

正如一些回复所指出的那样,这个问题在某种程度上没有得到充分的说明,只会引起问题的回应,而不是答案。只从一个方面来说:

这个问题将语言和语言实现合并在一起——这个C程序比c#程序慢2194倍,快1.17倍——我们不得不问你:哪种语言实现?

然后是哪些项目?哪个机器?哪些操作系统?哪个数据集?

其他回答

We have had to determine if C# was comparable to C++ in performance and I wrote some test programs for that (using Visual Studio 2005 for both languages). It turned out that without garbage collection and only considering the language (not the framework) C# has basically the same performance as C++. Memory allocation is way faster in C# than in C++ and C# has a slight edge in determinism when data sizes are increased beyond cache line boundaries. However, all of this had eventually to be paid for and there is a huge cost in the form of non-deterministic performance hits for C# due to garbage collection.

根据我的经验(这两种语言我都用过很多),与c++相比,c#的主要问题是内存消耗高,而且我还没有找到控制它的好方法。最终导致。net软件变慢的是内存消耗。

另一个因素是JIT编译器不能提供太多时间来进行高级优化,因为它在运行时运行,如果花费太多时间,最终用户会注意到它。另一方面,c++编译器有足够的时间在编译时进行优化。恕我直言,这个因素远没有内存消耗那么重要。

在一个特殊的场景中,c++仍然占据上风(并且将在未来几年占据上风),即可以在编译时预先确定多态决策。

通常,封装和延迟决策是一件好事,因为它使代码更加动态,更容易适应不断变化的需求,并且更容易作为框架使用。这就是为什么在c#中面向对象编程是非常高效的,并且它可以在术语“泛化”下泛化。不幸的是,这种特殊的泛化在运行时是有代价的。

Usually, this cost is non-substantial but there are applications where the overhead of virtual method calls and object creation can make a difference (especially since virtual methods prevent other optimizations such as method call inlining). This is where C++ has a huge advantage because you can use templates to achieve a different kind of generalization which has no impact on runtime but isn't necessarily any less polymorphic than OOP. In fact, all of the mechanisms that constitute OOP can be modelled using only template techniques and compile-time resolution.

在这种情况下(不可否认,它们通常局限于特殊的问题领域),c++胜过c#和类似的语言。

据我所知…

你的困难似乎在于决定你所听到的是否可信,当你试图评估这个网站上的回复时,这个困难将会重复。

你将如何判断人们在这里所说的比你最初听到的更可信还是更不可信?

一种方法是要求提供证据。

当有人声称“在某些领域c#被证明比c++快”时,问他们为什么这么说,让他们给你看测量结果,让他们给你看程序。有时他们只是犯了一个错误。有时你会发现他们只是在表达一种观点,而不是分享一些他们可以证明是正确的事情。

通常情况下,信息和观点会混淆在人们的说法中,你必须试着分清哪个是哪个。例如,从这个论坛的回复中:

"Take the benchmarks at http://shootout.alioth.debian.org/ with a great deal of scepticism, as these largely test arithmetic code, which is most likely not similar to your code at all." Ask yourself if you really understand what "these largely test arithmetic code" means, and then ask yourself if the author has actually shown you that his claim is true. "That's a rather useless test, since it really depends on how well the individual programs have been optimized; I've managed to speed up some of them by 4-6 times or more, making it clear that the comparison between unoptimized programs is rather silly." Ask yourself whether the author has actually shown you that he's managed to "speed up some of them by 4-6 times or more" - it's an easy claim to make!

毕竟,答案总要在某个地方,不是吗?:)

嗯,没有。

正如一些回复所指出的那样,这个问题在某种程度上没有得到充分的说明,只会引起问题的回应,而不是答案。只从一个方面来说:

这个问题将语言和语言实现合并在一起——这个C程序比c#程序慢2194倍,快1.17倍——我们不得不问你:哪种语言实现?

然后是哪些项目?哪个机器?哪些操作系统?哪个数据集?