Speeding up type converters

Posts   
 
    
mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 11-Jan-2016 09:20:11   

I see that the usual TypeConverter code checks for type based on Type.FullName string. This made me wondering whether this is the fastest way to do it. It turns out, based on a simple test, that it could be roughly 19 times faster if compared against actual types instead. Below is my test case. On my computer first method ends after 574ms while the improved one takes 34ms (1m iterations).


private static bool Compare(Type destinationType)
        {
            switch (destinationType.FullName)
            {
                case "System.Int64":
                case "System.Int32":
                    return true;
                default:
                    return false;
            }
        }

        private static bool Compare2(Type type)
        {
            return type == typeof(long) || type == typeof(int);

        }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 11-Jan-2016 10:19:45   

what typeconverter/code are you referring at exactly?

Frans Bouma | Lead developer LLBLGen Pro
mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 11-Jan-2016 13:09:25   

Otis wrote:

what typeconverter/code are you referring at exactly?

Well, for one the one from documentation: https://www.llblgen.com/documentation/4.2/SDK/gui_implementingtypeconverter.htm

But I think others out of the box use the same approach (comparing FullName).

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 12-Jan-2016 18:12:44   

Yeah I can create a static hashset of types in a static ctor, as they're always the same regardless and check the type with that. Will do that.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 20-Jan-2016 14:56:41   

The one in the runtime contains typecode checks, which is faster than string comparisons. So the current code is optimal enough. The documentation has to be updated with that though.

Frans Bouma | Lead developer LLBLGen Pro