|
发表于 2004-11-8 14:11:00
|
显示全部楼层
这个问题前一段时间有个争论的。有关C#和C++效率问
Hi guys,
I was wondering the difference of speed between C# and C++ for a long time ago.
Now I have THE answer. I made a program in C++ and after in C# and believe me, I take few months to write both. The benchmark is only mathematics calculation (intersection ray/plane, edge /sphere, vertex / sphere) and data structure traversal (edges/vertex/triangles). The result makes me with no voice:
55 seconds for C# against 8 seconds in C++ !!!
(This result exclude the model loading and octree partitioning)
I wasn't surprised so much because of the VM behind dotnet.
But really I expected less difference because of the hudge positive comments on "C#" regarding the performances.
Has anyone made also a test to compare ?
kind regards,
Epsilon
================================================
.NET 1.0 and 1.1 have unoptimized floating point support.
Although I have not benchmarked it personally, I am told that the .NET 2.0 beta is highly optimized for floating point and even takes advantage of CPU specific instructions.
This means that calculations should become on-par or even faster than those in a regular machine code compiled language like C++
===============================================
Considering that you are running an 8 second test, it makes sense. If you were to start timing as you begin the C++ compilation and then end when the program exits then you would get an idea of how the two times relate. With C# you are factoring in the compilation time (one of the reasons that C# has the fastest compiler that i have ever seen, seconds for compilation that would take days with C++). The .NET Framework uses JIT or Just-in-time compilation. This means that it compiles the "bytecode" or MSIL into binary and optimizes it for the current CPU before executing it. If you want to get a decent comparison, run the app for more time then a couple seconds (for instance a game wouldn’t run for just a couple seconds). Also the second time that you run the app is much quicker JIT'ing then the first. .NET languages do not have a "VM" they have the .NET runtime which JIT's the parts of the assembly that are being used. It is also very handy for optimization and the runtime allows many useful features like .NET Security, marshalling, reflection, dynamic source compilation, dynamic serialization using reflection, cross-platform deployment etc. These are things that you cant find in a native language and are well worth a couple seconds overall that you might loose each time you a run an app. The framework is also the most extensive, flexible and well-documented that i have ever seen (which is suppressing since it is written by Microsoft but is nevertheless true). The .NET framework is also entirely open-source unlike Java.
Also consider the fact that you are comparing simple mathematical calculations. You are comparing the #1 most difficult thing to implement as efficiently as it could be found in a native language. Also did you optimize the apps for both languages or not, because that will make a big difference? Now if you want to take everything into consideration instead of using a high-specialized test which caters to C++. Consider an entire application such as Axiom and OGRE. The Axiom C# port of the famous OGRE engine actually outperformed its C++ counterpart. It is also much smaller and more maintainable, written much more quickly, is much easier to read and understand, much more extensible, and the same build can be deployed an nearly any OS without and the code-base requires not IF-DEF's and maintenance of different builds. I would consider the entire package and not just a simple test which as NomadRock had put it is very limited in its scope. If you wait for .NET 2.0, you will see what has been tested to be an outstanding 5x speed boost in this already incredibly fast engine. Java even supports JIT'ing now. I don’t see why people blindly consider C# to be too slow a language to accomplish anything. This is the same thing that was C was considered to be in relation to Assembly back in the day. C# and the .NET framework make an excellent choice for game development for the aforementioned strengths especially due to maintainability, cross-platform deployment, and rapid application development even if it was to be much slower. But it may take a while because of the stigma associated with managed languages.
I will port my application in Java to make a benchmark.
I use Java at work and I know very well the speed of Java.
Also I have already done a renderer in Java : z-buffering, triangle filling
I think the language itself is faster than .NET
but all graphical things or some specifical areas seems to be a bit faster in C#
but you must know that all the Java libraries are written in Java, instead of C# which relies a lot on native library.
We will see in few weeks with the benchmark of my application.
=================================================
Much of the .NET framework is written in C#, but yes there is some interop (especially in the way of WinForms, but then Java again does the same with Swing). MSIL is actually fare more full-featured and optimized then the java byte-code which takes a somewhat less effective approach to many things. Of course this makes sense since MS simple took the idea of Java and improved on them. There have been some extensions in the Java bytecode with 1.5, but it still supports many legacy operations (I havent studied it too deeply, so correct me if im wrong). Also by default Java does *not* JIT its classes. It only does this if a JITer plugin is present and - depending on the environment - this option is selected manually. JIT'ing allows C# to run like actual native code (which is one of the reasons its *often* similiar to C++ in speed). There are of course cases with very mathimatical algorithems in which - for one reason or another - the code does not run as fast as C#. I know that the GC is not the problem since it rarely runs and allows C# to be faster in certain instances (ie vs. 'new' memory allocation in C++ as found in the DirectX demos). I would be interested in finding out what the problem is. Was you demo compiled as using checked operations? Also it could be the use of unmanaged code and pointers that speeds of certain things (such as array access). Also I know that the ArrayList - a very common data structure indeed - is a fairly inefficient class even when compared to the Java implementation.
======================== |
|