user925777
user925777

Reputation:

GetType() == type performance

I am making a XNA game and I am calling following code 2 to 20 times per update. I tried googling and it seems like this is semi-slow, so I just thought I'd ask if there is any faster way to compare types?

Code:

    public Modifier this[Type type]
    {
        get
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i].GetType() == type)
                {
                    return this[i];
                }
            }

            throw new NotImplementedException("Fix this");
        }

        set
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i].GetType() == type)
                {
                    this[i] = value;
                }
            }

            if(System.Diagnostics.Debugger.IsAttached)
                System.Diagnostics.Debugger.Break();
        }
    }

This code is in ModifierCollection class which inherits from a List. Modifier is a part of particle engine. Also, my game isnt in condition where I can actually test this yet so I cant test this, but this should work right?

I read something about RunTimeTypeHandles which should be faster, should I use it?

EDIT: What I am aiming to do with this is that I can do the following:

    (particleEffect["NameOfEmitter"].Modifiers[typeof(SomeCoolModifier)] as SomeCoolModifier).Variable = Value;

Basically I just want to change the value of some Modifiers in runtime.

EDIT 2: I just realized that I can just save the reference of Modifier to the class where I am at the moment calling this :P Maybe not as clean code if I have 5-10 modifiers but should remove this problem.

Upvotes: 1

Views: 571

Answers (3)

Cole Campbell
Cole Campbell

Reputation: 4867

If you don't need any of the extra functionality exposed by Type, and you're only concerned with absolute equality between types--i.e., you don't need to support inheritance--RuntimeTypeHandle is the fastest way to do this comparison.

Really, though, I would question whether this isn't a weakness of your class design. Unless you have a compelling reason to check the type directly, it's probably better to expose some sort of value (probably an enum) on your objects that represents what they are, and do your comparisons against that.

Upvotes: 4

TrueWill
TrueWill

Reputation: 25523

You could store the values in a dictionary indexed by type rather than a list so you wouldn't have to do an O(n) iteration over the list each time.

As noted in the comments, this does depend on the size of n and may be a micro-optimization. I'd recommend profiling your application.

Upvotes: 0

Robert Levy
Robert Levy

Reputation: 29073

If you want to be really fast and can trust the code that is calling you, change the indexer to just take an int. Then in whatever method (which you didn't show) that callers use to add Types to the list, return back to them the corresponding int. It's a worse API but it means you don't have to do any loops or lookups.

Upvotes: 0

Related Questions