AnsweredAssumed Answered

What is the fastest way to get a static attribute value from multiple Elements

Question asked by p.clemme on May 19, 2016
Latest reply on May 19, 2016 by bshang

Hi there

I writing a custom datareference making timeseries based on eventframes.

The dataference is rolling up a result on multiple child element.

As part of the dataference I need to get a static attribute value from each of the child element

I would have thought that getting the eventframes would be the slowest part, but it actually turns out to be getting the static values.

I have tried many ways to get the attribute values, but cannot seem to get to perform.

 

Is there a fast way to get a static attribute value from multiple elements fast?

 

I have added some stopwatches to my code to see the performance of difference parts of the code.

 

The last method using attributeTemplate, elements and LoadElement, seems to be the fastest, but I was wondering if there is a faster way to get all the attributes from the elements.

1.5 second for finding the instantiated attributes of a type in a single query is pretty slow.

The elements are all based on the same template.

 

Using  find elementAttributes

TOC Get Attributes: 00:00:01.4334836

TOC Get Attribute Values: 00:00:00.0131367

TOC Create Dict: 00:00:00.0005128

 

public static Dictionary<string, DateTime> ElementTocDictionaryFind(AFDatabase afDatabase, string elementName, string elementCategory, string attributeName)

 

var stopWatch = new Stopwatch();

 

var elementTocDictionary = new Dictionary<string, DateTime>();

 

//Get all elements

 

var afAttributes = new AFAttributeList();

 

var elementTemplate = afDatabase.ElementTemplates["WtgBaseTemplate"];

 

var splitAttributeName = attributeName.Split('|');

 

var lastAttributeName = splitAttributeName[splitAttributeName.Length-1];

 

var category = afDatabase.ElementCategories[elementCategory];

 

AFAttribute.FindElementAttributes(afDatabase, null, elementName, category, elementTemplate, AFElementType.Any, lastAttributeName, null,

 

TypeCode.DateTime, true, AFSortField.ID, AFSortOrder.Ascending, Int32.MaxValue);

 

Debug.WriteLine(string.Format("TOC Get Attributes: {0}", stopWatch.Elapsed));

 

AFValues values = afAttributes.GetValue();

 

Debug.WriteLine(string.Format("TOC Get Attribute Values: {0}", stopWatch.Elapsed));

 

for (int i = 0; i < afAttributes.Count; i++)

 

Convert.ToDateTime(values[i].Value);

 

Debug.WriteLine(string.Format("TOC Create Dict: {0}", stopWatch.Elapsed));

 

return elementTocDictionary;

 

 

using findelements

TOC Get Elements: 00:00:00.2474593

TOC Load Attributes: 00:00:00.2793046

TOC Get Attributes: 00:00:00.0043015

TOC Get Attribute Values: 00:00:00.0132460

TOC Create Dict: 00:00:00.0000945

 

public static Dictionary<string, DateTime> ElementTocDictionaryElementTemplate(AFDatabase afDatabase, string elementName, string elementCategory, string attributeName)

 

var stopWatch = new Stopwatch();

 

var elementTocDictionary = new Dictionary<string, DateTime>();

 

//Get all elements

 

var elements = Util.GetElements(afDatabase, null, elementCategory, elementName);

 

Debug.WriteLine(string.Format("TOC Get Elements: {0}", stopWatch.Elapsed));

 

AFAttributeTemplate attributeTemplate = null;

 

var elementTemplate = afDatabase.ElementTemplates["WtgBaseTemplate"];

 

List<AFAttributeTemplate> loadTemplateList = new List<AFAttributeTemplate> { attributeTemplate };

 

AFElement.LoadAttributes(elements, loadTemplateList);

 

Debug.WriteLine(string.Format("TOC Load Attributes: {0}", stopWatch.Elapsed));

 

var afAttributes = new AFAttributeList();

 

foreach (var afElement in elements)

 

var attribute = afElement.Attributes[attributeName];

 

Debug.WriteLine(string.Format("TOC Get Attributes: {0}", stopWatch.Elapsed));

 

AFValues values = afAttributes.GetValue();

 

Debug.WriteLine(string.Format("TOC Get Attribute Values: {0}", stopWatch.Elapsed));

 

for (int i = 0; i < afAttributes.Count; i++)

 

Convert.ToDateTime(values[i].Value);

 

Debug.WriteLine(string.Format("TOC Create Dict: {0}", stopWatch.Elapsed));

 

return elementTocDictionary;

 

 

 

Outcomes