How to speed up type checking

Posts   
 
    
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 31-Dec-2004 00:52:11   

Ok, I read in MSDN magazine that to optimize performance you should be familiar with how high level code gets compiled into MSIL and what the potential impacts are. Having over 4000 classes in .NET gives developers many ways to do the same thing, but some ways can be more efficient than others, take the following trivial test cases:

Test 1

        private void SlowCode(string x)
        {
            Random rnd = new Random();
            int number = 0;


            for (int i = 0; i < 5000000 ; i++)
            {
                // here is the inefficient way
                if(x.Equals("Hello world"))
                {
                    number = rnd.Next();
                }
            }
        }

Test 2

        private void BestPracticesCode(string x)
        {
            Random rnd = new Random();
            int number = 0;

            for (int j = 0; j < 5000000 ; j++)
            {
                // here is the best practices way
                if(x != null && x == "Hello World")
                {
                    number = rnd.Next();
                }
            }

        }

Ultimately the 2 tests do the same thing, i.e. check a value and do some work when a condition is true, and here are the results in milliseconds after executing both tests 10 times:

 SlowCode():0.511
BestPracticesCode():0.25


SlowCode():0.461
BestPracticesCode():0.25


SlowCode():0.471
BestPracticesCode():0.25


SlowCode():0.471
BestPracticesCode():0.25


SlowCode():0.471
BestPracticesCode():0.25


SlowCode():0.461
BestPracticesCode():0.25


SlowCode():0.471
BestPracticesCode():0.24


SlowCode():0.471
BestPracticesCode():0.25


SlowCode():0.471
BestPracticesCode():0.24


SlowCode():0.471
BestPracticesCode():0.24

So my question is this: is there a more effecient way to write this code:

 if (stateManager.CurrentState().Equals(typeof(FirstStep))

CurrentState() is a method that has a return type of System.Type and we are checking it to see if it is the same type as the FirstStep class. FirstStep is also a sub class of StateBase.

Ultimately what I need to do is a switch on the result of CurrentState(). For each case in the switch I need to execute seperate logic.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39801
Joined: 17-Aug-2003
# Posted on: 31-Dec-2004 11:23:49   

I use Type.UnderlyingSystemType.FullName, for example in TypeDefaultValue

Because you're then using a switch with strings, the C# compiler will create a hashtable behind the scenes and the switch will be very fast.

Reflection on types takes a hit, but only the first time, the next time it's pretty fast.

.Equals() is sometimes slower than '==' but can have different results than '=='. For strings, the compilers create hacky code behind the scenes (as the string is treated as a value type, but it is a reference type, so stringa.Equals(stringb) should give a different result than stringa == stringb) )

Frans Bouma | Lead developer LLBLGen Pro