还是现在反过来了?

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

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


当前回答

我发现2020年4月读过:https://www.quora.com/Why-is-C-so-slow-compared-to-Python,作者是一位拥有15年以上软件开发经验的现实世界的程序员。

它指出,c#通常较慢,因为它被编译为公共中间语言(CIL),而不是像c++那样的机器代码。然后,CIL通过公共语言运行库(CLR)输出机器代码。但是,如果您继续执行c#,它将获取机器代码的输出并缓存它,以便为下次执行保存机器代码。总而言之,如果多次执行,c#会更快,因为它是多次执行后的机器代码。

也有评论说,一个优秀的c++程序员可以做一些耗时的优化,但最终会被优化。

其他回答

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

另一个因素是JIT编译器不能提供太多时间来进行高级优化,因为它在运行时运行,如果花费太多时间,最终用户会注意到它。另一方面,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!

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#程序慢2194倍,快1.17倍——我们不得不问你:哪种语言实现?

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

In theory, for long running server-type application, a JIT-compiled language can become much faster than a natively compiled counterpart. Since the JIT compiled language is generally first compiled to a fairly low-level intermediate language, you can do a lot of the high-level optimizations right at compile time anyway. The big advantage comes in that the JIT can continue to recompile sections of code on the fly as it gets more and more data on how the application is being used. It can arrange the most common code-paths to allow branch prediction to succeed as often as possible. It can re-arrange separate code blocks that are often called together to keep them both in the cache. It can spend more effort optimizing inner loops.

我怀疑。net或任何jre都能做到这一点,但早在我上大学的时候就有人在研究这一点,所以认为这类东西很快就会在现实世界中找到自己的方式也不是不合理的。