c# - Speed of property dictionary slower than using a switch statement -
I currently have a class 'rising visual model' on which it has approximately 20-30 properties, which is 10,000+ Bar. I am generating various data.
Initially, I had a way of getting the property values arising from an XML string:
public object GetArisingPropertyValue (string propertyName) {Switch (Property Name) {Case "name_1": return name 1; Case "another_name": return anotherName; // etc}}
But in relation to other parts of the project, things were customized to simplify things and use a property dictionary to make life easier. So I have set my property dictionary as:
Private static readonly IDictionary & lt; String, string & gt; PropertyMap; Fixed ArisingViewModel () {PropertyMap = New Dictionary & lt; String, string & gt; (); Var myType = typeof (Emerging View Model); Foreach (MyType.GetProperties on in PropertyInfo (BindingFlags.Instance | BindingFlags.Public)) {if (propertyInfo.GetGetMethod () = Null!) {Var attr = propertyInfo.GetCustomAttribute & LT; FieldNameAttribute & gt; (); If (atr == empty) will continue; PropertyMap.Add (attr.FieldName, propertyInfo.Name); }}
I apply the application for any relevant propertys like this:
[filename ("name_1")] public string name1 {get {return _arisingRecord} .Name1; }}
and then search the property name / value using the following methods:
public static string GetPropertyMap (string groupingField) {string propName; PropertyMap.TryGetValue (Groupingfield, Out Propaneme); Return promotion; // will return the tap if the map is not found. } Public static object GetPropertyValue (object obj, string propertyName) {obj.GetType () back. GetProperty (propertyName) .GetValue (obj, null); }
My point is, I think that processing is a significant amount quickly using the old switch statement (how long the system is taking it to measure a lot Using simple timer class only) - 20 seconds vs ~ 25 seconds).
Can someone suggest what I am doing wrong, or any way of improving the current code?
I recommend implementing the same category from the C # Port String Template 4 (BSD 3-Clause License) do. Performance of template rendering pipelines - This functionality is heavily used in important parts and profiling indicates that the current implementation performs well.
This class uses a proper efficient caching and dispatch mechanism, although this can be improved using the ConcurrentDictionary
and the lock
statement Removing .NET 4+ for users.
You might find FindMember
to apply specific arguments to your properties with attributes.
Comments
Post a Comment