Reputation: 2354
I am currently developing a C# .net xna game engine.
I have been trying to figure out a way to have an update manager / scheduler / event system. I currently am using delegates to provide a way to create dynamic scheduled tasks and events.
I have recently read that delegates can be slow. The delegates in my game are being invoked every frame and I was wondering if there can be a performance hit from that?
Update:
I also just found this Link
This is what I was worried about, and I guess there can be a way around it. Thanks for all the other information.
Upvotes: 5
Views: 1820
Reputation: 5056
Your guesses (and our guesses) will not be accurate, and will not necessarily match your game's actual performance. The only way to know the actual performance hit would be with at least one kind of profiling. Why measure when you can guess?
Besides getting an actual measurement of your performance, consider: does it matter? If your game runs at 60fps (I even enjoy myself at 30fps on fast-paced games, and I can deal with as low at 20fps if it's slower, like a turn-based game) and you're capable of hitting 300fps in variable time-step mode, and the delegates cost you a whole 20 frames (they probably won't, by the way)... your game can still maintain 60fps in fixed time-step mode.
Upvotes: 1
Reputation: 16519
I will echo everyone else's answers here, in that delegates are not a problem unless they are a problem -- profilers are your friend.
One thing to watch out for that could be a problem is if you are re-creating the delegates every frame -- in which case, you could be generating excess garbage, which will reduce your performance. However, use tools like the CLR profiler to determine first if this is a problem for you.
Upvotes: 1
Reputation: 27429
There's obviously some perf hit with a delegate vs. a direct method call, but with modern (read: post v1.1) versions of the CLR, it is about as fast as a method call via an interface.
Here is a table of rough perf measures: http://msdn.microsoft.com/en-us/magazine/cc507639.aspx
As always, you should measure to see if performance is acceptable to you. As I've used delegates in performance-critical code (animation) and not experienced problems, I'd expect it to work out ok for you.
Upvotes: 1
Reputation: 106401
Don't worry about it - delegates are slightly slower than a regular function call but unless you're calling them several million times a second I very much doubt that you would notice.
I'd suggest sticking with delegates unless it proves to be a bottleneck.
Upvotes: 8
Reputation: 161831
Regardless of whether there can be a performance hit, the better question is whether or not there is a performance hit. You should measure the performance of the application with and without the delegates to determine whether any performance hit is acceptable.
Upvotes: 1