Why C# XNA When Everyone Wants C/C++

Started by
164 comments, last by Promit 15 years, 1 month ago
Quote:
Assemblies contain executable code in the form of Intermediate Language (IL) instructions, and symbolic information in the form of metadata. Before it is executed, the IL code in an assembly is automatically converted to processor-specific code by the Just-In-Time (JIT) compiler of .NET Common Language Runtime.


The above quote is directly copied from C# Language Specification 3.0 Document. Apparently this is how most c# programs are intended to run. The native assembly thing you are talking seems to be an extra feature of Visual Studio or other external tools, I don't know. But Java can also be compiled to native code when you sacrifice platform independence, so I'll take those as exceptions (you may not do so).

Quote:
I'd respect your statements and beliefs more if they were based off of anything more than pure hearsay, but that's not the case to the best of my knowledge. If you have any hard data backing up what you're saying, I'd be happy to take a look at it.


Both of our opinions are based on our observations and experiments. I haven't seen you come with any hard data so far either, maybe I just missed. So what makes your experiences more valuable than mine? I hope you see where this issue boils down to? I know I haven't addressed all points in your post but I see no point in doing so as it will bloat this thread even more than it is now.
Advertisement
Quote:Original post by xylent
Quote:
Assemblies contain executable code in the form of Intermediate Language (IL) instructions, and symbolic information in the form of metadata. Before it is executed, the IL code in an assembly is automatically converted to processor-specific code by the Just-In-Time (JIT) compiler of .NET Common Language Runtime.


The above quote is directly copied from C# Language Specification 3.0 Document. Apparently this is how most c# programs are intended to run. The native assembly thing you are talking seems to be an extra feature of Visual Studio or other external tools, I don't know.
It's built into the Microsoft implementation of the runtime; Mono supports it as well. Note that the standard doesn't say at runtime, it merely says before execution. It's practically a statement of the obvious -- IL doesn't run.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:
I believe that we can say it is easier to write slow (resource inefficient, in general) programs to write in languages like c# not solely because of the language itself but also because many c# programmers tend to think that the .NET (or whatever underlying platform) will handle too much for them whereas many c++ programmers know that they have the control, hence are responsible for certain things.

I would disagree, but I think you're getting warmer.

C++ doesn't by itself give you much in the way of control. Remember that C++ defines its own abstract machine -- it's own definition of what, exactly a byte is, and what memory is, and so on. Just like the CLR. The difference is that C++ leaves huge swaths of that abstract machine undefined or implementation defined. This is one of the reason we tend to recommend against C++ for beginners, because this is a massive drawback of C++. However, in the right contexts, it's also a huge benefit. When you combine all the implementation-defined bits with an extremely fixed platform (console, embedded system) and some documentation on how that implementation-defined behavior works, you get a fair modicum of control -- in these very isolated scenarios, you get the 'low level' ideal that many people believe C++ gives you overall. But only in these scenarios.

These scenarios are useful for console developers -- working on the GBA, for example, involves loads of implementation defined behavior -- you cast specific addresses to pointers and write to them, which anywhere else is broken code. But the platform implementing C++ on the GBA assures you that these memory locations are mapped to suitable hardware memory registers.

A lot of the problems arise because surprisingly few people actually realize what's going on there, and where the boundaries all lie, and as such extrapolate from the basis that 'it works like this on my machine' to 'C++ says it works like this.' Some even take so far as 'computers work like this,' which is wrong. Those are often the guys who you see claiming C# and such are nonviable because they don't "have pointers" (disregarding C#'s unsafe-mode access to pointers, for a moment) while failing to realize that its referential semantics they're really thinking of. Et cetera.

Those guys (who in my observations are often C++-only or otherwise very heavily-C++-focused) do tend to write poorly performing C# code, or otherwise poorly performing code in other languages, because they have yet adapted to the idea that different contexts call for different actions to achieve optimal performance.

C# and the CLR don't magically make all your resource management needs go away, for example. The GC typically handles memory, and nothing else, leaving you to manage other resources (file handles being the common example) yourself. And lack of widespread deterministic scope-based cleanup makes this more difficult, sometimes, than in C++.

C# and the CLR are not a silver bullet. To write optimally performing C#, you need to work at it, you need to understand your domain. This is no different from in C++ or any other language. The reasons many people find C# to be more optimal in terms of development time tend to be due to it providing better, higher level features and more modern concepts (of which the GC is just a small part). Not because of memory management and other things which the programmer must 'control' and 'manage' when writing native code (although of course, not having to do that is a boon, since often native implementations end up looking very similar to many of the things GCs do, such as pooling).

Writing code from the hip where performance isn't a concern one way or another is about as easy in C# and C++, as long as you know both languages well and are only talking about the handling of those things like memory -- std::vector goes a long way. It's usually the higher level stuff that really makes C# shine in terms of development efficiency.

Quote:
Both of our opinions are based on our observations and experiments. I haven't seen you come with any hard data so far either, maybe I just missed. So what makes your experiences more valuable than mine? I hope you see where this issue boils down to? I know I haven't addressed all points in your post but I see no point in doing so as it will bloat this thread even more than it is now.

I think you're unfairly conflating things here. Drilian's challenge to you vis-a-vis hard facts was (or appears to have been) in direct response to your assertion that I quoted above in this post, and responded to. For that you have no factual basis (and in fact have several facts slightly off-base, as I noted).

[Edited by - jpetrie on March 4, 2009 6:19:07 PM]
C++ is a bad language for beginners, because beginners are not the target audience of C++. There's too many things you can get wrong without the compiler or the runtime system telling you exactly what it is. Experts don't get these things wrong, but beginners do.
To clarify:

I'm not arguing that C# can't be slower than C++. I'm arguing that it is not ALWAYS slower, as many people seem to be stating. Good C# can be within the same performance ballpark as good C++ (i.e. close enough that the difference, positive or negative, between the two is negligible), even with something as performance-sensitive as games.

My assertion is logically weaker by definition (not to be confused with factually weaker). It's a "can be" rather than "always." "Always" is a ridiculously difficult position to demonstrate, hence my insistence on evidence.

As per my evidence, I cannot actually PHYSICALLY SHOW YOU (mostly because I've since partially-dismantled my port to try to add some features and never got around to finishing), but when I ported Mop of Destiny from C++ to C# (XNA, specifically, and when I say "ported" I really mean "rewrote the whole freaking thing from the ground up"), the game ran at the same rough speed when framerate limiting was disabled (+/- 20fps, in the 1500fps range, i.e. the game itself was running REALLY. FAST. as it's written to run at 60). I feel like this is an apples-to-apples comparison. Code written in C++, and code written in C#, using the same algorithms, each one written as best as I could in the given language.

Hence, my assertion of "C# and C++ perform CAN roughly the same, when care is taken in both."

The assertion I'm specifically challenging, however, was:

Quote:
...c# code will always be slower than c/c++ code (if both are optimal)


Which would require a substantial amount of backup. Since it's not likely that you HAVE a substantial amount of backup (and because I feel like I have a fairly good counter-claim), I believe that assertion to be false.

Quote:Original post by DevFred
C++ is a bad language for beginners, because beginners are not the target audience of C++. There's too many things you can get wrong without the compiler or the runtime system telling you exactly what it is. Experts don't get these things wrong, but beginners do.

I can tell you, based on code that I've seen (and, regrettably, written), that non-beginners can (and do) make the same mistakes that beginners can (and do), regardless of skill level. "Only human" and all that. :)
To reply to the OP, I would suggest that you do not try to plunge into both Cpp and game programming at the same time (as a beginner). Since you have a basis in C#, you will find learning the specifics of game architecture with it much faster than if you are also wrestling with Cpp. Even if you were experienced in both, I would suggest C# (as I have used both) for learning as it is just that much more productive.

You will find that your messing around with example apps and small demos to be a much quicker and clearer experience with C#, as you will be spending less time wrestling with mysterious memory allocation issues and any of the other gotchyas that Cpp introduces. C# is more than adequate to allow you to learn the skills needed for game programming. Once you have these skills down, there's absolutely no reason why you can't learn and switch to Cpp if you wish to "mix it with the big boys".

I guess the basis of my argument is that I think taking on game programming and learning Cpp simultaneously will take MUCH more time than learning them sequentially.

There's my $0.02AUD. [grin]
Quote:Original post by DevFred
Experts don't get these things wrong


Not in my experience...
There are lots of reasons to use C#.

I use it because I got tired of the things I didn't like about C++, and C# is what I always wanted, a simpler C++.

I use it because I get things done a lot faster, and I have fun doing it.

I use it because I can make my own games that run good enough for me, and then later I can sell them in the XBOX Live shop if I want. As opposed to trying to get a job in a bigger company and working on someone else's stuff.

I use it because I can gloss over the technical details of getting all the core systems running like video, sound, input, timing, and whatever else I am forgetting.

Beginner should use it because it's a great IDE/API combo, and there is a ton of domain and API specific tutorials and books for it.

It can be a lot easier to get help. When you post on the XNA forums, everyone is using the same API, IDE, Controller, Platform, etc. All techniques you read about are immediately usable and don't have to be translated to a new API's way of doing things.

There are a ton of tutorials, starter kits, and developer blogs, all XNA specific.
Quote:Original post by Bru
Quote:Original post by Imgelling
"but also the fact that c# is bound to windows"

That is not a fact, http://mono-project.com/Main_Page

tbh even if you can use this mono thing to run .net stuff on non windows systems, you can still say c# is bound to windows. if you make a serious and big game i am not sure you want to make the clients install stuff like that(beside i am not sure its if its very legal advertise mono with your game this way.
dont think i am a complete idiot, i am not talking with confidence just what i belive might be true.


Mono is installed by default on many modern Linux distributions (not all), for those that doesn't have it you can simply add it as a dependancy in the package system and it will be dealt with automatically. For Mac i don't expect things to be that much harder.

As far as the legal part of things, just read the Mono and Microsoft section here:
http://mono-project.com/FAQ:_General

It seems as if Microsoft are extremely open when it comes to C# and the CLI, the only thing that would make them more open would be if they decided to give away the code to their own implementation.
(They won't do this though, not as long as they're able to keep their own implementation competetive)

MS .NET is ahead of mono both in features and in performance, but this is no different than Microsofts C++ compiler being ahead of gcc/g++ and shouldn't be a major issue.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
these are my favourite threads :)

Quote:Which would require a substantial amount of backup. Since it's not likely that you HAVE a substantial amount of backup (and because I feel like I have a fairly good counter-claim), I believe that assertion to be false.

actually the chief inventor of C# himself claims that c# is slower (you can find a link to this in one of previous posts or have a google)

heres a piece from one of the few commercial games I know written in c#
Quote:A lot of times the natural expressions you'd want to use when programming in C# will cause unseemly hitches when garbage collects on the 360, and you're probably going to want to multithread what would, if you had written in C++ in the first place, probably run fine entirely on one core.
The end result is the time you save prototyping and getting up-and-running in the first place can be lost in working around performance later.


http://www.gamasutra.com/view/feature/3796/postmortem_torpex_games_schizoid.php?print=1

This topic is closed to new replies.

Advertisement