Sign in to follow this  

[.net] How fast is "is"?

This topic is 4613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this