还是现在反过来了?
据我所知,c#在某些领域被证明比c++更快,但我从来没有勇气亲自测试它。
我想你们任何人都可以详细解释这些差异,或者告诉我有关信息的正确位置。
还是现在反过来了?
据我所知,c#在某些领域被证明比c++更快,但我从来没有勇气亲自测试它。
我想你们任何人都可以详细解释这些差异,或者告诉我有关信息的正确位置。
当前回答
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.
其他回答
这要看情况。如果字节码被转换成机器代码(不仅仅是JIT)(我的意思是如果你执行程序),如果你的程序使用了很多分配/释放,它可能会更快,因为GC算法只需要一次(理论上)通过整个内存,但正常的malloc/realloc/free C/ c++调用会在每次调用上引起开销(调用开销、数据结构开销、缓存丢失;))。
所以这在理论上是可能的(对于其他GC语言也是如此)。
我并不认为不能在大多数应用程序中使用c#元编程有什么极端的缺点,因为大多数程序员都不使用它。
另一个很大的优势是SQL,像LINQ“扩展”一样,为编译器提供了优化数据库调用的机会(换句话说,编译器可以将整个LINQ编译为一个“blob”二进制文件,其中调用的函数内联或为您的使用优化,但我只是在这里推测)。
快了5个橘子。或者更确切地说:不可能有一个(正确的)笼统的答案。c++是一种静态编译语言(但也有配置文件引导的优化),c#在JIT编译器的帮助下运行。它们之间的差异如此之大,以至于像“快了多少”这样的问题都无法回答,甚至无法给出数量级。
通常,这取决于应用程序。在某些情况下,c#可能慢得可以忽略不计,而在其他情况下,c++要快5到10倍,特别是在操作可以轻松SIMD的情况下。
没有严格的理由说明为什么基于字节码的语言(如c#或Java)不能像c++代码一样快。然而,c++代码在很长一段时间内都要快得多,今天在许多情况下仍然如此。这主要是因为更高级的JIT优化实现起来比较复杂,而真正酷的JIT优化现在才出现。
所以在很多情况下,c++更快。但这只是答案的一部分。c++实际上更快的情况是高度优化的程序,其中专业程序员彻底优化了代码。这不仅非常耗时(因此非常昂贵),而且由于过度优化通常会导致错误。
On the other hand, code in interpreted languages gets faster in later versions of the runtime (.NET CLR or Java VM), without you doing anything. And there are a lot of useful optimizations JIT compilers can do that are simply impossible in languages with pointers. Also, some argue that garbage collection should generally be as fast or faster as manual memory management, and in many cases it is. You can generally implement and achieve all of this in C++ or C, but it's going to be much more complicated and error prone.
As Donald Knuth said, "premature optimization is the root of all evil". If you really know for sure that your application will mostly consist of very performance critical arithmetic, and that it will be the bottleneck, and it's certainly going to be faster in C++, and you're sure that C++ won't conflict with your other requirements, go for C++. In any other case, concentrate on first implementing your application correctly in whatever language suits you best, then find performance bottlenecks if it runs too slow, and then think about how to optimize the code. In the worst case, you might need to call out to C code through a foreign function interface, so you'll still have the ability to write critical parts in lower level language.
请记住,优化一个正确的程序相对容易,但更正一个优化的程序要难得多。
给出实际的速度优势百分比是不可能的,这在很大程度上取决于你的代码。在许多情况下,编程语言实现甚至不是瓶颈。请带着极大的怀疑态度使用http://benchmarksgame.alioth.debian.org/上的基准测试,因为这些测试的主要是算术代码,很可能与您的代码完全不同。
C/ c++在有大型数组或数组(任何大小)上的大量循环/迭代的程序中可以表现得更好。这就是为什么在C/ c++中图形化通常要快得多,因为几乎所有的图形化操作都基于繁重的数组操作。net在数组索引操作中是出了名的慢,这是由于所有的安全检查,这对于多维数组尤其如此(是的,矩形c#数组甚至比锯齿形c#数组还要慢)。
The bonuses of C/C++ are most pronounced if you stick directly with pointers and avoid Boost, std::vector and other high-level containers, as well as inline every small function possible. Use old-school arrays whenever possible. Yes, you will need more lines of code to accomplish the same thing you did in Java or C# as you avoid high-level containers. If you need a dynamically sized array, you will just need to remember to pair your new T[] with a corresponding delete[] statement (or use std::unique_ptr)—the price for the extra speed is that you must code more carefully. But in exchange, you get to rid yourself of the overhead of managed memory / garbage collector, which can easily be 20% or more of the execution time of heavily object-oriented programs in both Java and .NET, as well as those massive managed memory array indexing costs. C++ apps can also benefit from some nifty compiler switches in certain specific cases.
I am an expert programmer in C, C++, Java, and C#. I recently had the rare occasion to implement the exact same algorithmic program in the latter 3 languages. The program had a lot of math and multi-dimensional array operations. I heavily optimized this in all 3 languages. The results were typical of what I normally see in less rigorous comparisons: Java was about 1.3x faster than C# (most JVMs are more optimized than the CLR), and the C++ raw pointer version came in about 2.1x faster than C#. Note that the C# program only used safe code—it is my opinion that you might as well code it in C++ before using the unsafe keyword.
Lest anyone think I have something against C#, I will close by saying that C# is probably my favorite language. It is the most logical, intuitive and rapid development language I've encountered so far. I do all my prototyping in C#. The C# language has many small, subtle advantages over Java (yes, I know Microsoft had the chance to fix many of Java's shortcomings by entering the game late and arguably copying Java). Toast to Java's Calendar class anyone? If Microsoft ever spends real effort to optimize the CLR and the .NET JITter, C# could seriously take over. I'm honestly surprised they haven't already—they did so many things right in the C# language, why not follow it up with heavy-hitting compiler optimizations? Maybe if we all beg.