views:

135

answers:

5

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 http://blogs.msdn.com/b/shawnhar/archive/2007/07/09/delegates-events-and-garbage.aspx

This is what I was worried about, and I guess there can be a way around it. Thanks for all the other information.

+1  A: 

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.

John Saunders
I will do so and let you know
Chris Watts
+7  A: 

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.

mikera
yeah they should only be getting called at most 60 times a second
Chris Watts
There will be microseconds difference if it's only 60 times/second. I doubt you'd notice much difference between delegates and functions if it was 6000 times/s.
Igor Zevaka
@Chris Yeah, if it is 60 times per second then the performance impact will be completely negligible. If the delegates work and keep the code simple and maintainable, then go for it.
mikera
Alright great thanks. thats exactly what I was hoping to hear =)
Chris Watts
+1  A: 

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.

codekaizen
Ya that table helps thanks!
Chris Watts
+1  A: 

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.

Brian S
+1  A: 

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.

Blair Holloway