5

I wrote a small library in Python for handling the xbox 360's STFS files to be used on my web applications. I would like to rewrite it for use in the many desktop programs people are writing for 360 game modding, but I'm not quite if I should continue using C# or delve into C++ or even C.

STFS is an in-file file system used by the xbox 360 and the job of the library would be extracting/injecting files, which could take noticeable amounts of time to do. What I know in C# comes from internet tutorials and resources, as would anything I learn about C++, so what I'm asking is if it's better to bring myself to a slightly lower-level language without knowing beforehand the features of the language that increase performance, or continue assuming that compiler optimizations and that my lack of experience will mean that the language I choose won't matter.

3
  • 2
    Consider my article on how to choose a programming language: mortoray.com/2012/05/29/how-to-choose-a-programming-language Jun 13, 2012 at 11:53
  • 3
    Performance is not the only reason one may choose C++ over C#.
    – quant_dev
    Jun 13, 2012 at 12:31
  • 3
    I don't think you should be touching your code in any language until you understand why your current code is not fast enough. breakout the profiler first.
    – jk.
    Jun 21, 2012 at 14:39

8 Answers 8

19

The advantage of C# over C++ is the speed and momentum in development (unless you're one of the very few C++ gurus, who can claim to do competitive RAD in C++ and not hurt themselves in the process; but then again good luck finding teammates who can reasonably claim the same thing). Compiler optimizations won't help you code faster, and with fewer errors, especially coming from a Python background.

You can tweak your performance if it is really an issue, by writing critical portions of code in C++/CLI, or even C with PInvoke, but - if you're in the Microsoft world - there will usually be a place for C# code high up on your stack, while C/C++ may be completely absent in some solutions. Therefore, perhaps you should start with C#, and complement with C++, not viceversa.

3
  • Hadn't thought of that, but it sounds like a good idea. I'll get everything working in C# and if I have any performance issues I'll see if I'm able to write it better in c++ or c. Thank you!
    – mowwwalker
    Jun 13, 2012 at 6:50
  • 3
    C# can also use unsafe blocks Jun 13, 2012 at 7:04
  • 3
    I agree with the statement here, so +1, but maybe it would be good to add that if you set priorities to features of your app, then maybe you'll discover that in your case performance (whatever this mean) is more important than writing the code fast (whatever this imply) and in this specific case, you'd better let open all possibilities you can for optimization, in which case any managed language just cannot compete with C++ (or C but it don't have higher abstraction optimizations...). Note that this case is not that common, fortunately.
    – Klaim
    Jun 13, 2012 at 7:10
6

You should first consider if the languages you're already comfortable with are suitable for the task. If you conclude that you know no languages that are well-suited for the task, then you should investigate which would be the best to learn.

From the brief description you gave, I'd say C# is well suited for desktop development and XBOX integration. So why waste time and effort learning C++ (with its steep learning curve)? But if your requirement is for the program to be potentially portable, for example, C# immediately loses points. So make sure you define your requirements to yourself properly.

5
  • 4
    Mono pretty much makes C# as portable as C++.
    – mattnz
    Jun 13, 2012 at 6:51
  • 4
    @mattnz, depends whether you can accept 100MB of dependencies or not. If you're writing a trivial command-line application with no real need for external dependencies then C++ is far more portable than C# Jun 13, 2012 at 11:08
  • 4
    C++ has just as much dependencies, they just are already installed. Though 'potentially portable' is one of the key YAGNI features. Make it work first.
    – Telastyn
    Jun 13, 2012 at 11:20
  • 4
    @mattnz Not really. C++ almost certainly available on your non-Microsoft game console, on your DVR, on your router and on a million other devices that Mono has not been ported to. Jun 13, 2012 at 16:01
  • 2
    In context of the question - he specifically said "..for use in the many desktop programs...". I was not aware a DVR, router or millions of other devices were considered desktops.
    – mattnz
    Jun 13, 2012 at 23:11
5

If your app deals with a lot of handles and connections you may prefer C++ for it's RAII resource management. Garbage collectors don't work for handles to shared resources. If you are using C# or java you are forced to de-volve into the old "malloc" style all over again with explicit release calls.

C++ deals with resource management in a cleaner and more elegant way that is not possible in other languages. C++ is light years ahead of C#. And there's no sign of C# or Java catching up as their best answer to RAII is "using" blocks which are not RAII, just a syntactic sugar over an explicit try/finally/close. It does not have the built in fail-safe of popping off the stack. ie you must remember to try/close it just as you always did, but with a nicer syntax.

I've seen countless C# and java systems fail because developers left connections and handles to the garbage collector...

5
  • 3
    Please clarify why the "using" block is not RAII, and/or what you mean by "it does not have the built in fail-safe of popping off the stack" - you left me in the dark here. Note that "using" will guarantee deterministic disposal if the block is exited normally, or with an exception.
    – vski
    Jun 13, 2012 at 11:36
  • 4
    the using block is a 'manual' RAII - ie you have to remember to put it in where its needed. Which takes you down to the level of C programmers manually calling malloc and free on memory. Using is not a replacement for RAII therefore, its actually manual memory management.
    – gbjbaanb
    Jun 13, 2012 at 13:39
  • 1
    I see. Well, that's the problem when you have both deterministic (D) and non-deterministic/garbage collected (ND) disposal, you have to flag one of them somehow - but that doesn't mean that it's not scope-bound resource management (or RAII). C# has implicit ND, explicit (with using) D disposal, C++/CLI has implicit D (for the unmanaged stuff, they're having problems with RAII and IDisposable), explicit (^ types) ND, vanilla C++ has implicit D disposal, and a big empty void where ND should be. It's a question of taste, and my taste says give me GC/ND for "manual" RAII any time.
    – vski
    Jun 13, 2012 at 14:03
  • +1 for being the only one to answer the question. ie a reason to use C++ besides performance.
    – mike30
    Jun 13, 2012 at 20:35
  • @vski: A using block is only usable when the lifetime of an object is bound to the current scope. It's not much help in preventing resources from leaking when constructors throw exceptions. Actually, C# seems somewhat hostile to programmers trying to prevent leaks in such cases, since the cleanest thing to do would be to call Dispose on the partially-constructed object, but there's no way to get it.
    – supercat
    Oct 6, 2012 at 10:05
3

This sounds like an extreme form of premature optimisation to me. Don't do it. As @vski said - (Assuming the choice is C#,C++ or C) stick with C# and use to C++ in the one place of code where (in the extremely unlikely event ) that it might make a difference.

Edit : In response to the comment by @gbjbaanb :

This also leads to the question - "Is rewriting you Python App really required or is it optimizing prematurely."

2
  • stick with C#? His app is written in python.
    – gbjbaanb
    Jun 13, 2012 at 13:40
  • 1
    The question asked specifically about C#, C++ or C. I answered that question. The one that has not been asked - "Should I rewrite my python app" - is a different question, to which you comment is valid.
    – mattnz
    Jun 13, 2012 at 23:14
2

I will choose first the tool that will make it easier to develop and to maintain (C#). As other said, there are options to optimize later on if needed (and you can optimize inside C#).

2

It's not given that your code will run faster all by itself just by using a lower level language like C or C++. Python or C# should fare just fine, and with good code can be nearly as fast as C or C++ code. If your code is too slow, then you can profile it to see what parts to optimize, but don't worry about that when choosing what language to use for implementation.

Instead of switching languages you'd probably fare much better spending some effort on buying a book for further education on the one you already know or best practices, profiling etc.

One thing that may be worth consideration though, is portability. Using C or C++ your code can be more portable, and have less dependencies on runtime environments etc. This may be of value to others using your code, as they don't have to install a big runtime environment or language just to run your program. If you assume most of your users will have these parts in place anyways, this also becomes a moot point.

1

From your description, it sounds as if the bottleneck for performance here will be in I/O, and when it comes to the reads/writes here, neither language will show any significant difference. So if the performance of the intermediary code of modifying the data is your concern for performance, you could probably be modifying any read data while you continue to read more in a separate thread, making the choice of language here for performance fairly moot, IMO.

-2

I encourage you to write it in assembly lol. Bigger learning curve but if it is speed you want well written assembly can provide it.

The next runner up in speed would be C. Though everyone says that it is not that much faster than C++ it is not the only benefit. C is IMO has been written better and has much more potential for an increase in speed. Classes in C++ add quite a bit of overhead and unneeded complexity of learning the language. Without them you might as well be writing C and get that increase in speed.

Though I have not written any C# or even seen the source code for a C# program I know I don't like it as I'm a Linux guy. Thus why I strongly do not recommend C#. This sounds like it would be a useful program o many Linux users as well.

Python seem like a good choice though to me it seems more unstable due to the high level of abstraction for a lower level function. It would allow it to be moved back and forth between Linux Windows and Mac.

1
  • Please leave comments with negative feedback especially in as I do not see why I have had 2 on this answer. The only think of is because I prefer lower languages? I find that offensive as I do not down people for liking higher languages. In this response speed seems important so I chimed in.
    – Joe
    Jun 21, 2012 at 19:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.