[.net] How fast is "is"?

Started by
1 comment, last by Fruny 18 years, 10 months ago
Hey all, I'm working on a message based game engine, and I'm having handlers identify their messages with the "is" keyword. How much of a performance hit is this going to cost me? Here's basically what the heirarchy looks like:

IMessage
   IEntityMessage
      CreateEntityMessage
      KillEntityMessage
   ISoundMessage
      PlaySoundMessage
Every message is posted to a singleton Message Centre and the handlers subscribe to that centre to listen for messages. This gives the added benefit of handlers able to react to certain things. For example, the sound message might have a location and the entity handler might have AI react to the sound. Here's the gist of how I check message types:

	public class EntityHandler : BaseMessageHandler
	{
		public static EntityCreateMessage createMessage = new EntityCreateMessage();

		public override void Receive(IMessage message)
		{
			if(message is EntityCreateMessage)
			{
				EntityCreateMessage msg = (EntityCreateMessage)message;

				System.Windows.Forms.MessageBox.Show("Message Received!");
			}
		}

	}
I'm thinking that the "if(message is EntityCreateMessage)" is really going to end up being a bottleneck, since it's a lot of boxing / unboxing comparisons. Potentially, 100s of messages could be flying around every second. Any ideas?
Advertisement
Profile it and see. is is going to use basic reflection to determine if the type is of the type specified. But short of profiling it IN the environment, you won't really know if it's going to be a bottleneck.

Just throwing a few hundred messages at it at once is not a way to profile, you must make sure that the test case matches the environment fairly closely.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Check out the Visitor design pattern.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement