游戏开发论坛

 找回密码
 立即注册
搜索
楼主: wingser

选择C#的理由

[复制链接]

28

主题

685

帖子

703

积分

高级会员

Rank: 4

积分
703
发表于 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.
========================

28

主题

685

帖子

703

积分

高级会员

Rank: 4

积分
703
发表于 2004-11-8 14:11:00 | 显示全部楼层

Re:选择C#的理由

详情请看:
http://www.devmaster.net/forums/index.php?s=3849f527e4a6ef22658d8ac95715e241&showtopic=8&pid=13298&st=60&#entry13298
顺便了解一下普遍与人家的差距。

28

主题

685

帖子

703

积分

高级会员

Rank: 4

积分
703
发表于 2004-11-8 14:12:00 | 显示全部楼层

Re:选择C#的理由

让某些井底之蛙见识一下

41

主题

340

帖子

345

积分

中级会员

Rank: 3Rank: 3

积分
345
发表于 2004-11-8 15:12:00 | 显示全部楼层

Re:选择C#的理由

在langhorn受控环境中,C#比C++更有优势。除非你绕开WGF另用C++编一个API接口,可能微软不给你这种机会。

28

主题

685

帖子

703

积分

高级会员

Rank: 4

积分
703
发表于 2004-11-8 15:16:00 | 显示全部楼层

这倒是。

不过C#确实比C++要强些。要方便得多。

netcat现在做啥呢?

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
 楼主| 发表于 2004-11-8 16:44:00 | 显示全部楼层

Re:选择C#的理由

好哟
早知道国外opensource阵营和MSfans势不两立
不想国内也是如是
不过c/C#来比可没什么可比性,一个是较为底层的,一个是高级语言
稍微会点java和c#的人都知道,和c#相比的应该是java,不管是语法上,垃圾收集上,虚拟机机制上,c#都是模仿java的
SevenCat不妨给我们讲讲透彻这个现在困扰全世界程序员的问题吧?

28

主题

685

帖子

703

积分

高级会员

Rank: 4

积分
703
发表于 2004-11-8 16:52:00 | 显示全部楼层

惭愧,我只知道能用C#的时候最好不要用C++

rt

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
 楼主| 发表于 2004-11-8 17:07:00 | 显示全部楼层

Re:选择C#的理由

:)
我前一阵一直在寻找一门系统的高级语言来学习,不外乎是java和c#
我比较关注平台可移植,因为我使用opengl作为图形api,而不是只能用于windows的dircectx
我觉得MS开发任何东西都没有关注windows以外的平台,

听说.net也是只能在windows上跑?不知是否?我的意思是.net的ide。。。

另外,作为一个程序员应该是尊重正版的,当然我不否认d版对中国的it有过积极作用
然后问题就是,我没有$500,那还不算一个windows OS的钱,so......

41

主题

340

帖子

345

积分

中级会员

Rank: 3Rank: 3

积分
345
发表于 2004-11-8 18:14:00 | 显示全部楼层

Re:选择C#的理由

如果只关注平台的可移植性,而不强调程序的执行效率,那java是首选;如果程序的执行效率比平台可移植性更注重的话,C++是首选(这也是目前游戏开发的主流);如果既要考虑平台的可移植性,又要考虑程序的执行效率的话,C#就是首选。这是我最近搞网络游戏模拟器的体会,正好这3种语言都用过。不过,目前成功的商业网游大作,大都是采用C++和Delphi的。

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
 楼主| 发表于 2004-11-8 19:07:00 | 显示全部楼层

Re:选择C#的理由

c#的虚拟机目前也只有一个非官方的mono啊,最近才刚1.0版,而且我在一些c#论坛上看来,反应也不理想

至于执行效率,楼上可不可以说说,好在哪,比如举一些各具体的数据

新版java据官方说执行效率已有改进

因为基于同样底层原理的关系,我认为c#并不会比java更快

c++虽快而不能速成,自己构造写大东西要有很多年经验,如果有c++由java和c#那么易用不出bug,那么全世界都c++罗
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-10 17:28

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表