Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# delegate performance in xna game

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.

like image 205
Chris Watts Avatar asked Jul 09 '10 00:07

Chris Watts


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


3 Answers

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.

like image 128
mikera Avatar answered Oct 14 '22 18:10

mikera


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.

like image 35
John Saunders Avatar answered Oct 14 '22 18:10

John Saunders


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.

like image 36
codekaizen Avatar answered Oct 14 '22 19:10

codekaizen