C # delegate effect in xna game

I am currently developing a C # .net xna game engine.

I am trying to figure out a way to create an update manager / scheduler / event system. I am currently using delegates to provide a way to create dynamic scheduled tasks and events.

I read recently that delegates can be slow. The delegates in my game call every frame, and I was wondering if there could be a performance hit from this?

Update:

I also found this http://blogs.msdn.com/b/shawnhar/archive/2007/07/09/delegates-events-and-garbage.aspx

This is what I was worried about, and I think there might be a way around this. Thanks for all the other info.

+5
source share
5 answers

Donโ€™t worry about this - delegates are a bit slower than a normal function call, but if you do not call them several million times per second, I doubt very much what you noticed.

I suggest sticking with delegates if this does not become a bottleneck.

+8
source

Regardless of whether there can be performance, the best question is whether there is a performance hit. You should measure application performance with and without delegates to determine if any click on performance is acceptable.

+1
source

Obviously, some kind of perfection with a delegate or a direct method call, but with modern versions of the CLR (read: post v1.1), is about as fast as a method call through the interface.

Here is a table of rude performative measures: http://msdn.microsoft.com/en-us/magazine/cc507639.aspx

As always, you should measure to make sure that performance is acceptable to you. Since I used delegates in performance-critical code (animations) and had no problems, I would expect it to work fine for you.

+1
source

Your guesses (and our guesses) will not be accurate and will not necessarily correspond to your game. The only way to find out the actual performance hit is at least one type of profiling. Why measure when you can guess?

Besides getting the actual measurement of your performance, consider: does it matter? If your game runs at 60 frames per second (I even enjoy 30 frames per second in fast-paced games, and I can handle such a slow speed of 20 frames per second if it is slower, like a turn-based game) and you are able to hit 300 frames per second in variable time-step mode, and delegates will cost you a total of 20 frames (by the way, they probably won't) ... your game can still support 60 frames per second in a fixed time mode.

+1
source

I will answer all the other answers here, as delegates are not a problem, if they are not a problem - profilers are your friend.

One thing to keep track of this might be a problem if you re-create delegates in each frame, in which case you could generate extra garbage, which would decrease your performance. However, use tools such as the CLR profiler to determine first if this is a problem for you.

+1
source

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


All Articles