Property dictionary speed is slower than using switch statement

I currently have an ArisingViewModel class that contains about 20-30 properties that will be checked over 10,000 times when I generate various data.

In the beginning, I had a way to extract the values ​​of emerging properties from an XML string line by line:

    public object GetArisingPropertyValue(string propertyName)
    {
        switch (propertyName)
        {
            case "name_1":
                return Name1;
            case "another_name":
                return AnotherName;

            // etc.
        }
    }

      

But it was adapted to use a property dictionary to make it easier to update and make life easier for other parts of the project. So I set up my property dictionary like this:

    private static readonly IDictionary<string, string> PropertyMap;


    static ArisingViewModel()
    {
        PropertyMap = new Dictionary<string, string>();

        var myType = typeof(ArisingViewModel);

        foreach (var propertyInfo in myType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
        {
            if (propertyInfo.GetGetMethod() != null)
            {
                var attr = propertyInfo.GetCustomAttribute<FieldNameAttribute>();

                if (attr == null)
                    continue;

                PropertyMap.Add(attr.FieldName, propertyInfo.Name);
            }
        }
    }

      

I apply attributes to any relevant properties like this:

    [FieldName("name_1")]
    public string Name1
    {
        get
        {
            return _arisingRecord.Name1;
        }
    }

      

And then find the property names / values ​​using the following methods:

    public static string GetPropertyMap(string groupingField)
    {
        string propName;
        PropertyMap.TryGetValue(groupingField, out propName);
        return propName; //will return null in case map not found.
    }

    public static object GetPropertyValue(object obj, string propertyName)
    {
        return obj.GetType().GetProperty(propertyName).GetValue(obj, null);
    }

      

My problem is that I find that the processing is significantly faster using the old switch statement (using a very simple timer class to determine how long the system takes - ~ 20 seconds versus ~ 25 seconds).

Can anyone suggest what I am doing wrong, or any ways to improve the current code?

+3


source to share


1 answer


I propose to implement a class similar to ObjectModelAdaptor

from the C # port for StringTemplate 4 (BSD 3-clause license). This functionality is heavily used in mission-critical templating pipeline environments, and profiling shows that the current implementation is performing reasonably well.

This class uses a fairly efficient caching and dispatch mechanism, although it can be improved for .NET 4+ users with ConcurrentDictionary

and removing operators lock

.



You probably want to change the implementation FindMember

to implement your property-specific logic with attributes.

+1


source







All Articles