What are the performance implications of these C # features?

I developed a component-based game library, with the general intention of writing it in C ++ (as is the case with my strengths), and Ogre3D as the source. Now that I’m really ready to write some code, I thought that it would be faster to test my infrastructure under XNA4.0 (it’s somewhat faster to get results / write an editor, etc.). However, although I am not new to C ++ or C #, I am a little new when it comes to doing β€œXNA” things, so to speak, so I had a few queries before I started knocking out code:

  • I read about using arrays rather than collections to avoid performance hits, and also read that it wasn’t quite so, and if you listed, say, a specific collection List<> (unlike IEnumerable<> ), the enumerator is the type of value that is used for each iteration, and that there is no problem with the GC. This article was returned in 2007. Does this mean, or are you having trouble with XNA developers? Ideally, I would like to go down the chosen route until I do too much.

  • If arrays really are a way out, no questions asked, I assume that when it comes to resizing the array, do you copy the old with the new space? Or does it not matter? Are you trying to never, never resize an array? Wouldn't GC be used for the old, if so, or is it uninteresting?

  • Since the engine was developed for C ++, the design allows the use of lambda and delegates. One project uses the fastdelegate library, which is the fastest way to use delegates in C ++. A more flexible, but slightly slower approach (albeit barely noticeable in the C ++ world) is to use C ++ 0x lambdas and std::function . Ideally, I would like to do something similar in XNA and allow the use of delegates. Are delegates using any significant performance issues?

  • If there are performance considerations regarding delegates, is there a difference between:

     public void myDelegate(int a, int b); private void myFunction(int a, int b) { } event myDelegate myEvent; myEvent += myFunction; 

against

  public void myDelegate(int a, int b); event myDelegate myEvent; myEvent += (int a, int b) => { /* ... */ }; 

Sorry if I stumbled a bit, I prefer to be clear in my questions. :)

Thanks in advance!

+2
source share
1 answer

Basically the only performance issue you need to know about in C # that is different from what you should know in C ++ is the garbage collector. Just do not allocate memory during the main game cycle, and everything will be fine. Here is a blog post that goes into detail .

Now for your questions:

1) If the frame collection iterator can be implemented as a value type (without creating garbage), then it usually (always?) Was. You can safely use foreach , for example List<> .

You can check if you are assigned in your main loop using the CLR Profiler .

2) Use List instead of arrays. They will handle resizing for you. You must use the Capacity property to Capacity enough space before starting gameplay to avoid problems with the GC. Using arrays, you just need to implement all these functions yourself - ugly!

GC starts at distribution (but not at freeing up memory). On the Xbox 360, it runs for every 1 MB allocated and is very slow. On Windows, this is a bit more complicated, but also does not have such a huge impact on performance.

3) C # delegates are pretty damn fast. And faster than most people expect. They roughly coincide with method calls on interfaces. Here and here are questions that provide more comments about delegate performance in C #.

I could not say how they compare with C ++ parameters. You have to measure it.

4) No. I am sure that this code will produce an identical IL. You can parse it and check, or profile, though.

I can add - without testing myself - I suspect that having event myDelegate will be slower than normal myDelegate if you don't need all the magic of event .

+5
source

Source: https://habr.com/ru/post/900706/


All Articles